美文网首页
ToolBar学习笔记

ToolBar学习笔记

作者: 河婆墟邓紫棋 | 来源:发表于2016-12-11 22:49 被阅读18次

    《第二行代码》学习笔记--ToolBar

    一、简介
    ToolBar,是Material Design控件,在5.0之后引入,大大扩展ActionBar,更加灵活,直接继承的是ViewGroup。

    二、基本使用
    1.依赖:直接在build.gradle中添加V7兼容包

    compile 'com.android.support:appcompat-v7:23.4.0'
    

    这里版本号和compileSdkVersion版本相对应。

    2.Mianfest.xml

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity"
                android:label="首页">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN"/>
    
                    <category android:name="android.intent.category.LAUNCHER"/>
                </intent-filter>
            </activity>
        </application>
    

    在Mainifest.xml文件中,我们看到我们采用了AppTheme主题,我们先去Style.xml中去除ActionBar,具体如下:

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

    3.开始使用:首先在布局中添加ToolBar,并对ToolBar的一些属性进行说明:
    在根布局需要引入命名空间:

    xmlns:app="http://schemas.android.com/apk/res-auto"
    

    布局中添加:

    <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
    

    属性值中的“?”表示对Android系统的主题样式进行重用;
    android:theme 表示让ToolBar单独使用深色系主题;
    app:popupTheme 表示让弹出的菜单栏使用淡色系主题;

    刚刚程序中使用了淡色系主题,此时,ToolBar的字体将自动为深色系,就变成了黑色,丑了,单独为ToolBar改深色系,这样文字就变成了淡色系。但弹出框又变成了深色系,也就是弹出的时候背景都是黑黑的,此时我们再指定弹出框为淡色系,让文字又变为深色。

    没有指定theme和popupTheme 仅指定theme 指定theme和popupTheme 指定theme和popupTheme

    4.还需要新建一个menu才可以到上图的效果,menu如下:
    toolbar_menu.xml

    <?xml version="1.0" encoding="utf-8"?>
    <menu xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:android="http://schemas.android.com/apk/res/android">
    
        <item android:id="@+id/message" android:icon="@drawable/message" android:title="消息" app:showAsAction="always"/>
        <item android:id="@+id/skin" android:title="夜间模式" app:showAsAction="never"/>
        <item android:id="@+id/setting" android:title="设置选项" app:showAsAction="never"/>
    </menu>
    

    always 表示总是要显示菜单项,如果空间不够不显示;
    never 表示不显示菜单项;
    ifRoom 表示空间够才显示;

    5.java代码中,简单设置即可显示:

    setSupportActionBar(mToolbar);
    ActionBar supportActionBar = getSupportActionBar();
    if (supportActionBar != null) {
        supportActionBar.setDisplayHomeAsUpEnabled(true);
    }
    

    效果图:(标题显示了"首页",因为我们在Mainfest.xml中label属性设置了"首页")


    指定theme和popupTheme

    相关文章

      网友评论

          本文标题:ToolBar学习笔记

          本文链接:https://www.haomeiwen.com/subject/laopmttx.html