Android Toolbar 的使用

ActionBar 由于其设计的原因,被限定只能位于活动的顶部,从而不能实现一些Material Design的效果,因此官方现在已经不在建议使用ActionBar。更加推荐使用ToolBar。

使用ToolBar的前期准备:

在AndroidManifest.xml中的<application>中有个theme的@style/AppTheme。

我们需要在 res/valus/styles.xml修改<style name="AppTheme" parent="AppCompat.Light.DarkActionBar">

将其修改为Theme.AppCompat.NoActionBar(无标题栏 深色主题)或Theme.AppCompat.Light.NoActionbar(无标题栏  淡色主题)

代码式样如下,也可以对 colorPrimary 等3选项进行修改。除上述3个属性之外,我们还可以通过textColorPrimary、windowsBackground和navigationBarColor等属性来控制更多位置的颜色。特别提醒:colorAccent这个属性比较难理解,它是一个强调意思。

<resources>

    <!-- Base application theme. -->
    <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>

</resources>

然后修改activity_main.xml使用ToolBar控件

<android.support.v7.widget.Toolbar
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?attr/actionBarSize"<!将Toolbar设置为ActionBar的高度>
   android:background="?attr/colorPrimary"<!将Toolbar设置为淡色主题>
   android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
   app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/><!弹出菜单设置为淡色主题>


猜你喜欢

转载自blog.csdn.net/qq_41105058/article/details/80998648