【问题标题】:Action Bar (Toolbar) Icon操作栏(工具栏)图标
【发布时间】:2016-01-21 13:09:29
【问题描述】:

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

这是我的基础活动的一部分,我尝试了这些方法,没有任何变化:

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); 

MainActivity 的 XML 的一部分,其中我有工具栏,以及在框架布局中设置的 Activity 的所有内容:

<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">

操作栏上的导航抽屉效果很好,但是,我无法更改工具栏上的任何图标。

我在添加之前在工具栏上设置了图标,然后像操作栏一样添加它,效果很好,但我无法使用操作栏/工具栏设置标题或其他内容。

截图:

【问题讨论】:

  • 澄清你的问题
  • 添加这些行getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
  • 我已经有了这些行...参见代码。我的问题是我无法在操作栏/工具栏上设置任何内容。标题等...
  • 请张贴截图让大家看看发生了什么。
  • @HiI'mFrogatto,查看编辑,发布 SS。

标签: android


【解决方案1】:

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

getSupportActionBar().setTitle("Aaaaaaaa");

【讨论】:

    【解决方案2】:

    像这样试试。

    <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>
    

    Base Activity 中将标题设置为 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");
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多