2016-01-20 126 views
0

我有导航抽屉,为了在操作栏上显示它,我制作了自定义工具栏视图并将其设置为操作栏。现在我有箭头图标,我不能改变。操作栏(工具栏)图标

这是我基本活动的一部分,我想这些方法,没有什么变化:MainActivity在那里我有工具栏的XML的

protected void onCreateDrawer() 
    { 

     // R.id.drawer_layout should be in every activity with exactly the same id. 
     drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.tool_bar); 
     setSupportActionBar(toolbar); 

     drawerToggle = new ActionBarDrawerToggle((Activity) this, drawerLayout, R.drawable.ic_drawer, 0, 0) 
     { 
      public void onDrawerClosed(View view) 
      { 
       getSupportActionBar().setTitle(R.string.app_name); 
      } 

      public void onDrawerOpened(View drawerView) 
      { 

       getSupportActionBar().setTitle("Aaaaaaaaaaa"); 
      } 
     }; 
     drawerLayout.setDrawerListener(drawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 
     getSupportActionBar().setDisplayUseLogoEnabled(true); 
     getSupportActionBar().setLogo(R.drawable.ic_drawer); 
     getSupportActionBar().setDisplayShowTitleEnabled(true); 

组成部分,活动的所有内容框架布局设置:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="false"> 
    <!-- The main content view --> 


    <android.support.v7.widget.Toolbar 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/tool_bar" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:minHeight="?attr/actionBarSize" 
     android:background="?attr/colorPrimary" /> 


    <FrameLayout 
     android:id="@+id/frame_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="?attr/actionBarSize"> 

Navigation Drawer over Action bar很好用,但是,我无法更改工具栏上的任何图标。

我在工具栏上设置图标,然后添加它就像操作栏,它很好,但没办法,我可以设置标题或其他与ActionBar /工具栏。

截图:Screenshot

+1

澄清你的问题 –

+0

添加这些行'getSupportActionBar()setDisplayHomeAsUpEnabled(真)。 getSupportActionBar()。setHomeButtonEnabled(true);' –

+0

我已经有这些行...请参阅代码。 我的问题是,我不能在操作栏/工具栏上设置任何东西。标题等... – Sasaman

回答

0

使用toolbar.setTitle("Aaaaaaaaa")代替

getSupportActionBar().setTitle("Aaaaaaaa"); 
+0

试过...没有。 – Sasaman

0

尝试这样的。

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/custom_actionbar" 
    style="@style/ToolBarStyle" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/transparent" 
    android:minHeight="@dimen/abc_action_bar_default_height_material"> 

    <TextView 
     android:id="@+id/toolbar_title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:textAllCaps="true" 
     android:textColor="#ffffff" 
     android:textSize="18sp" 
     android:textStyle="bold" /> 

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

基本活动设置你的标题TextView的

Toolbar mToolbar =(Toolbar) findViewById(R.id.metro_toolbar_actionbar); 
TextView mTitle = (TextView) mToolbar.findViewById(R.id.toolbar_title); 
setSupportActionBar(mToolbar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setDisplayShowTitleEnabled(false); 
mTitle.setText("Your text");