【问题标题】:Clear old ActionBar items清除旧的 ActionBar 项目
【发布时间】:2018-04-14 02:46:08
【问题描述】:

我遇到的问题是我的应用没有操作栏。 我使用了自定义工具栏,并使用此代码设置工具栏

    private void setupToolbar(){
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("Shopping Cart");
    }
}

但结果显示一个操作栏,其中包含来自 mainActivity 的旧项目以及工具栏添加的项目。

编辑: 这看起来像:

所以你会看到这里显示的两个标题 我想删除在主要活动中创建的图标(后退图标除外)和主页标题

【问题讨论】:

  • 在“mainActivity 中的旧项目加上工具栏添加的项目”中,“项目”到底是什么意思?
  • 我已经编辑了问题以包含所发生情况的屏幕截图
  • 右侧的那些图标和 HOME 项是您的选项菜单。如果您不想要这些,请删除 Activity 中的 onCreateOptionsMenu() 方法。
  • 非常感谢我使用了invalidateOptionsMenu();,效果很好
  • 另一个问题请教如何制作一个在用户向下滚动时隐藏的平行工具栏

标签: android android-studio android-actionbar toolbar


【解决方案1】:
First you change style and select NoActionBar :
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
and set CustomActionbar android :like this
 <android.support.design.widget.AppBarLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <RelativeLayout
        android:id="@+id/toolbar_core"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
      <ImageView
          android:id="@+id/img_left_arrow"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:layout_gravity="center_vertical"
          android:layout_marginLeft="@dimen/_10sdp"
          android:background="@android:color/transparent"
          android:src="@drawable/ic_header_back"
          android:visibility="visible" />
      <LinearLayout
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_centerInParent="true"
          android:layout_gravity="center_vertical"
          android:gravity="center"
          android:orientation="horizontal">
        <TextView
            android:id="@+id/id_header"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:ellipsize="end"
            android:singleLine="true"
            android:text="Catagory"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/white"
            android:textSize="@dimen/_18sdp" />
      </LinearLayout>
      <ImageView
          android:id="@+id/Moreappps"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:layout_marginRight="@dimen/_10sdp"
          android:background="@android:color/transparent"
          android:src="@mipmap/ic_more_apps" />
    </RelativeLayout>
  </android.support.design.widget.AppBarLayout>
I hope its help you

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2020-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多