弃用actionbar 使用toolbar

89 阅读1分钟

步骤 踩坑

  1. 设置主题样式,在style文件中,没有actionbar的样式
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
  1. 第二步 设置toolbar的布局文件
<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/Theme.NavigationDemo.AppBarOverlay">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/Theme.NavigationDemo.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>
  1. 第三步 代码使用
setSupportActionBar(binding.toolbar)
后面可以 
 设置Title,menu ,Logo, subTitle 

image.png