Android Material Design之Toolbar

 

1.ActionBar

1.1 ActionBar概述

        ActionBar是系统原生的标题栏每个Activity默认的最顶部的那个标题栏就是ActionBar。

1.2 默认的使用方式

        ActionBar到底从哪里来呢?其实是根据项目中指定的主题来显示的。在AndridManifest.xml中默认引入的theme如下所示:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.luckyliuqs.materialdesign">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        ......
    </application>

</manifest>

          打开styles.xml文件如下所示:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
</resources>

        可以看到,这里引入的parent主题就是Theme.AppCompat.Light.DarkActionBar,这个DarkActionBar就是一个深色的ActionBar主题,默认出现的ActionBar就是因为指定了这个主题才出现的。

1.3 ActionBar缺陷

      ActionBar被限定只能位于Activity的顶部,而不能实现一些Material Design效果,因此官方已经不再建议使用ActinBar了。

 

2.ToolBar

2.1 概述

       ToolBar的强大之处在于,它不仅继承了ActionBar的所有功能,而且灵活性很高,可以配合其他控件来完成一些Material Design的效果。

2.2 ToolBar的引入方式

       通过在styles.xml中指定parent主题为不带ActionBar的主题。通常选择有Theme.AppCompat.NoActionBar和Theme.AppCompat.Light.NoActionBar这两种主题。

       1)Theme.AppCompat.NoActionBar

        表示深色主题,它会将界面的主体颜色设为深色陪衬颜色设为淡色。如下所示:

      2)Theme.AppCompat.Light.NoActionBar

       表示淡色主题,它会将界面的主体颜色设为淡色,陪衬颜色设为深色。如下所示:

        引入方式 如下所示:

<resources>
    <!-- Base application theme. -->
    <style name="ToolBarTheme" 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>

2.3 Activity界面color位置设置详解

       如下图所示:

         从上图可以看出,每个属性所指定的颜色的位置不一样。

 

 

3.ToolBar实例

3.1 实现效果

     实现的效果如下图所示:

 

3.2 添加action按钮

          右击res目录->new->Directory,创建一个menu文件夹,然后右击menu文件夹->new->Menu resource file,创建一个toobar.xml文件如下所示:每一个item就是一个action按钮

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/mail"
        android:icon="@drawable/ic_mail"
        android:title="邮箱"
        app:showAsAction="always"/>
    <item android:id="@+id/delete"
        android:icon="@drawable/ic_delete"
        android:title="删除"
        app:showAsAction="ifRoom"/>
    <item android:id="@+id/qq"
        android:icon="@drawable/ic_qq"
        android:title="QQ"
        app:showAsAction="never"/>
</menu>

        参数说明:

app:showAsAction=""

always:表示永远显示在ToolBar中,如果屏幕空间不够,则不显示。

ifRoom:表示屏幕空间足够的情况下显示在ToolBar中;不够则显示在菜单中。

never:表示永远显示在菜单中。

注意:ToolBar中的action只会显示图标,而菜单中的action只会显示title文字。

icon用于指定按钮的图标。
title用于指定按钮的文字。

3.3 设置布局样式

         在activity_mian.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <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.support.design.widget.FloatingActionButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_margin="35dp" />
</RelativeLayout>

        注意:这里使用了xmlns:app指定了一个新的命名空间。正是用于布局文件使用了xmlns:android来指定一个命名空间,才可以使用android:id、andrid:layout_width等等属性。

         由于Mater Design是在Android 5.0系统才出现的,所以很多Meterial属性在5.0之前的系统中并不存在,为了兼容之前的老系统,这里使用了xmlns:app,这样就可用使用app:attribute属性了。

 

3.4 在Activity中处理业务

        在MainActivity.java中:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        //设置支持的ActionBar
        setSupportActionBar(toolbar);

    }
    //加载toolbar.xml菜单文件
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.toolbar,menu);
        return true;
    }
    //处理各个按钮的点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.mail:
                Toast.makeText(this,"点击了邮箱图标",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this,"点击了删除图标",Toast.LENGTH_SHORT).show();
                break;
            case R.id.qq:
                Toast.makeText(this,"点击了QQ图标",Toast.LENGTH_SHORT).show();
                break;
        }
        return true;
    }
}

 

 

4.自定义Toolbar

4.1 自定义Toolbar样式

      可以自定义ToolBar,包括返回键样式,背景,高宽等。通过在布局文件中引入AppBarLayout,在AppBarLayout里面嵌套ToolBar,然后为Toolbar指定theme,在style.xml中自定义theme即可。

        效果图如下所示:自定义Toolbar的返回键颜色为白色,背景颜色为橙色,指定高度和标题

        布局文件如下所示:在AppBarLayout里面嵌套ToolBar

<android.support.design.widget.AppBarLayout
    android:id="@+id/abl"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    android:theme="@style/AppTheme.AppBarOverlay"
    app:elevation="1dp">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:theme="@style/theme_toolbar_white">

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

</android.support.design.widget.AppBarLayout>

     在style.xml中自定义theme:其中colorControlNormal指定返回键颜色

<!--toolbar样式-->
<style name="theme_toolbar_white" parent="ThemeOverlay.AppCompat.Dark.ActionBar">
    <item name="colorControlNormal">@color/white</item>
    <item name="android:actionBarSize">50dp</item>
    <item name="android:background">#FF5722</item>
</style>
    

     其部分属性如下所示:

colorControlNormal指定返回键的颜色
android:actionBarSize指定Toolbar的高度
android:background指定Toolbar的背景颜色

4.2 使用系统原生返回键

      效果图如下所示:

 

      代码如下所示:

public class BaseTitleActivity extends BaseCommonActivity{

    protected Toolbar toolbar;

    @Override
    protected void initViews() {
        super.initViews();
        toolbar = findViewById(R.id.toolbar);
        //设置标题栏
        setSupportActionBar(toolbar);
    }

    /**
     * 设置标题
     * @param title
     */
    @Override
    public void setTitle(CharSequence title) {
        if(!TextUtils.isEmpty(title)){
            super.setTitle(title);
        }
    }

    /**
     * 开启返回功能
     */
    protected void enableBackMenu(){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                break;
        }
        return super.onOptionsItemSelected(item);
    }
}

4.3 自定义返回键

     效果图如下:这里自定义设置返回键为+

      自定义返回键方式有两种,如下:

    1)在style.xml的自定义them中添加一个item,自定义返回键样式:

<item name="android:navigationIcon">@drawable/ic_add</item>

   2)在布局文件中的Toolbar中指定app:navigationIcon属性

app:navigationIcon="@drawable/ic_add"

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
和导航栏的联动,你需要在布局文件中添加一个SearchView和NavigationView,然后在代码中实现它们的联动。具体实现可以参考以下代码: 布局文件: ``` <androidx.drawerlayout.widget.DrawerLayout android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <androidx.appcompat.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:elevation="4dp" android:theme="@style/ThemeOverlay.AppCompat.ActionBar" /> <androidx.appcompat.widget.SearchView android:id="@+id/search_view" android:layout_width="match_parent" android:layout_height="wrap_content" android:iconifiedByDefault="false" android:queryHint="Search" /> <FrameLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> <com.google.android.material.navigation.NavigationView android:id="@+id/navigation_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" app:headerLayout="@layout/nav_header" app:menu="@menu/nav_menu" /> </androidx.drawerlayout.widget.DrawerLayout> ``` 代码实现: ``` // 设置Toolbar Toolbar toolbar = findViewById(R.id.toolbar); setSupportActionBar(toolbar); // 设置SearchView SearchView searchView = findViewById(R.id.search_view); searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() { @Override public boolean onQueryTextSubmit(String query) { // 处理搜索事件 return false; } @Override public boolean onQueryTextChange(String newText) { // 处理搜索框文本变化事件 return false; } }); // 设置NavigationView NavigationView navigationView = findViewById(R.id.navigation_view); navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { // 处理导航栏菜单点击事件 return false; } }); // 设置DrawerLayout DrawerLayout drawerLayout = findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawerLayout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawerLayout.addDrawerListener(toggle); toggle.syncState(); ``` 这样就可以实现SearchView和NavigationView的联动了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

luckyliuqs

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值