Android Toolbar 的使用

由于 Toolbar 更加自由、可控,Google 在逐渐使用 Toolbar 代替 Actionbar。

  • 要使用 Toolbar 必须引入 appcompat-v7 支持,在 styles.xml 中设置主题为 NoActionbar。
    api 'com.android.support:appcompat-v7:21.0.3'
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- toolbar颜色 -->
        <item name="colorPrimary">#4876FF</item>
        <!-- 状态栏颜色 -->
        <item name="colorPrimaryDark">#3A5FCD</item>
        <!-- 窗口的背景颜色 -->
        <item name="android:windowBackground">@android:color/white</item>
        <!-- SearchView -->
        <item name="searchViewStyle">@style/MySearchView</item>
    </style>

    <style name="MySearchView" parent="Widget.AppCompat.SearchView" />
</resources>
  • 添加 Toolbar 显示的标题和图标
mToolbar = (Toolbar) findViewById(R.id.toolbar);
mToolbar.setLogo(R.drawable.ic_launcher);
// 标题的文字需在setSupportActionBar之前,不然会无效
mToolbar.setTitle("主标题");
mToolbar.setSubtitle("副标题");
setSupportActionBar(mToolbar);
DrawerLayout 和最左边的 Toggle 的使用,直接上源码

Toolbar

<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar" >

</android.support.v7.widget.Toolbar>

DrawerLayout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <include layout="@layout/toolbar" />

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <!-- 内容界面 -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/holo_blue_light"
            android:orientation="vertical" >
            <Button
                android:layout_width="100dp"
                android:layout_height="match_parent"
                android:text="内容界面"/>
        </LinearLayout>

        <!-- 侧滑菜单内容 必须指定其水平重力 -->
        <LinearLayout
            android:id="@+id/drawer_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:orientation="vertical" >
            <Button
                android:layout_width="200dp"
                android:layout_height="match_parent"
                android:text="菜单界面"/>
        </LinearLayout>
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

MainActivity

mToolbar = (Toolbar) findViewById(R.id.toolbar);           
mToolbar.setLogo(R.drawable.ic_launcher);                  
// 标题的文字需在setSupportActionBar之前,不然会无效                      
mToolbar.setTitle("主标题");                                  
mToolbar.setSubtitle("副标题");                               
setSupportActionBar(mToolbar);                             
                                                           
getSupportActionBar().setDisplayHomeAsUpEnabled(true);     
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer);  
mDrawerToggle = new ActionBarDrawerToggle(                 
        this, mDrawerLayout, mToolbar,                     
        R.string.abc_action_bar_home_description,          
        R.string.abc_action_bar_home_description_format);  
mDrawerToggle.syncState();                                 
mDrawerLayout.setDrawerListener(mDrawerToggle);            
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值