2014-11-24 16 views
11

我一直在使用AppCompatv7中的最新工具栏lib.I已经在ToolBar ViewGroup中放置了一个textview并且我想从我的活动中的片段中为此Textview设置一个标题。一个自定义的操作栏((ActionBarActivity)getActivity).setcustomView(..)会完成这项工作。但由于使用这个工具栏我无法使用它。此外,我在我的BaseActivity中实现了一个方法,它被所有Activities.This BaseActivity包含我的方法来初始化一个滑动抽屉的左侧。我必须在活动中初始化initDrawerLayout()方法,否则抽屉将不会被初始化。如果我初始化它在片段它给我所有空结果,既不是抽屉的切换按钮也不是自定义标题被设置。从Android中的片段设置标题inToolBar

这是我initDrawer代码..

public void initDrawerLayout(String toolbar_text) { 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerRelative = (RelativeLayout) findViewById(R.id.drawer_relative); 
     if (mDrawerLayout != null) { 
      findViewById(R.id.drawer_btn_a).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_b).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_c).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_d).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_e).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_f).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_g).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_h).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_i).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_j).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_k).setOnClickListener(this); 
      findViewById(R.id.drawer_btn_l).setOnClickListener(this); 
      findViewById(R.id.my_layout).setOnClickListener(this); 





      Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); 
      toolbar.setBackground(getResources().getDrawable(R.drawable.icn_actionbar_background)); 
      TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title); 
      mTitle.setText(toolbar_text); 
      mTitle.setTypeface(Typeface.DEFAULT_BOLD); 
      if (toolbar != null) { 
       setSupportActionBar(toolbar); 
       getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      } 
      toolbar.setNavigationIcon(R.drawable.ic_drawer); 

      mDrawerToggle = new ActionBarDrawerToggle(
        this, mDrawerLayout, toolbar, 
        R.string.drawer_open, R.string.drawer_close 
       ); 
       mDrawerLayout.setDrawerListener(mDrawerToggle); 


      toolbar.setNavigationOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        if (mDrawerLayout.isDrawerOpen(Gravity.LEFT)) { 
         mDrawerLayout.closeDrawer(Gravity.LEFT); 
        } else { 
         mDrawerLayout.openDrawer(Gravity.LEFT); 
        } 
       } 
      }); 
      mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, 
        GravityCompat.START); 

      mDrawerLayout.setScrimColor(getResources().getColor(
        android.R.color.transparent)); 
      getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
      getSupportActionBar().setHomeButtonEnabled(true); 
      getSupportActionBar().setDisplayShowTitleEnabled(false); 

     } 
    } 

这我在片段代码..

((FirstActivity) getActivity()).initDrawerLayout(mFirst.name); 其中mFirst是类Person

和工具栏的代码的对象。 。

<android.support.v7.widget.Toolbar 
      android:id="@+id/my_awesome_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:minHeight="?attr/actionBarSize"> 
      <TextView 
       android:id="@+id/toolbar_title" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="center" 
       android:gravity="center" 
       android:text="Toolbar Title" 
       android:textColor="@color/action_text-color" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      </android.support.v7.widget.Toolbar> 

请帮帮忙..

+5

谁下来投this..can请你回答downvoting..I提出的问题一个经过相当多的搜索之后,问过这个问题。 – AndroidMech 2014-11-24 10:34:40

回答

6

要允许片段与其Activity进行通信(设置工具栏标题),您可以在Fragment类中定义一个接口并在Activity中执行该接口,如下所述:Communicating with Other Fragments

1

您可以更改工具栏的标题上的事件OnAttach,像这样

 var toolbar = activity.FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar); 
     toolbar.Title = "New Title"; 
19

我这样做是这样的:从尾数通话

getActivity().setTitle("your title"); 

您也可以调用任何你的父母活动的功能是这样的:

YourActivity mYourActiviy = (YourActivity) getActivity(); 
mYourActivity.yourActivityFunction(yourParameters); 
+5

[它为什么不起作用!](http://stackoverflow.com/a/26998718/3182574) – 2016-03-26 09:10:18

+0

它工作!谢谢...... – 2018-02-21 08:09:26

2

答案写下面在oncreateview片段的方法。

getActivity().setTitle("your name"); 
0

在您的活动中声明工具栏为公共。

public class YourActivity : Activity 
{ 
    public Toolbar toolbar; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     toolbar = .... // initilize your toolbar 
    } 
} 

然后,从片段

((YourActivity) getActivity()).toolbar.Title = "Your Title"; 
1

如果您使用的是自定义工具栏,这将帮助你:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar); 
toolbar.setTitle("Feedback"); 
+2

尽管这段代码可能会回答这个问题,但提供关于_how_和/或_why_的附加上下文可以解决问题,从而提高答案的长期价值。 – 2017-03-14 17:07:15

1

您需要设置标题的活动commiting片段并返回片段

getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment, mainFragment).commit(); 
      toolbar.setTitle("ShivShambhu"); 
      return contentFragment; 

这适用于我。

1

如果有人遇到这个问题,这可能会有用。

基本上,你有4个选项,如何处理:

  • 使用的接口,以便与您的活动,或任何其它方便的方法来进行通信,其事件总线。
  • 您致电getActivity().setTitle("Title"),但在这种情况下,您需要通过在您的活动中致电setSupportActionBar()Toolbar附加到ActionBar
  • 您可以拥有您的Toolbar的公共实例,并从该片段中访问该实例。
  • 最后,如果你需要你的Toolbar(你可能想做些别的事情用),你可以简单地把它这样的实例:

    Toolbar bar=Toolbar.class.cast(getActivity().findViewById(R.id.toolbar));

好了,最后一个选项只有在Toolbar尚未传递给setSupportActionBar方法时才能解决问题。

如果它已经,那么你需要调用这个方法在你的活动:

supportActionBar.setDisplayShowTitleEnabled(false)

这将解决这个问题。

不过,我会建议使用ButterKnife这将使得它一点点清洁,这里一个例子:

Toolbar actionBar=findById(getActivity(),R.id.actionBar); 
    actionBar.setTitle("Title"); 
1
getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { 
      @Override 
      public void onBackStackChanged() { 
       FragmentManager.BackStackEntry lastBackStackEntry=null; 

       int lastBackStackEntryCount = getSupportFragmentManager().getBackStackEntryCount() - 1; 
       if(lastBackStackEntryCount >= 0) 
       { 
        lastBackStackEntry = getSupportFragmentManager().getBackStackEntryAt(lastBackStackEntryCount); 
       } 
       Toast.makeText(MainActivity.this, ""+lastBackStackEntryCount, Toast.LENGTH_SHORT).show(); 
       if(lastBackStackEntryCount == -1) 
       { 
        toolbar.setTitle(""); 
        toolbar.setLogo(R.drawable.header_logo); 
       } 
       else 
       { 
        toolbar.setTitle(lastBackStackEntry.getName()); 
       } 
      } 
     }); 
+0

这也将处理第一个片段问题 – 2017-07-25 00:13:05

+0

这将有助于从后台堆栈标签本身加载标题,所以只需提供正确的标签名称也是您片段的标题 – 2017-07-25 00:15:09

0

XML

<android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar_detail" 
    android:layout_width="match_parent" 
    android:layout_height="55dp" 
    android:paddingLeft="10dp" 
    android:paddingRight="10dp" 
    app:contentInsetStart="0dp" 
    app:contentInsetStartWithNavigation="0dp" 
    android:background="@color/tool_bar_color"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="horizontal" 
     android:gravity="center"> 


     <TextView 
      android:id="@+id/toolbar_title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:textColor="#fff" 
      android:textSize="16sp" /> 




    </LinearLayout> 
</android.support.v7.widget.Toolbar> 

从工具栏查找TextView的标识

如果您正在使用A ctivity

TextView mTitle = (TextView)findViewById(R.id.toolbar_title); 
mTitle.setText("set your title"); 

如果你是期运用片段

TextView mTitle = (TextView) view.findViewById(R.id.toolbar_title); 
mTitle.setText("set your title"); 
+0

请不要发布重复的答案到多个问题。您的答案应该针对每个具体问题量身定制,并且相同的问题应该标记为重复。 – meagar 2017-10-06 12:36:55

1

这项工作对我来说:

Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.id_toolbar); 

toolbar.setTitle("New Title");