Android——MaterialDesign之一Toolbar

时间:2021-06-15 16:35:48

Toolbar

由于ActionBar设计原因只能存在活动的顶部,从而不能实现MaterialDesign的效果,现在推荐使用Toolbar,继承Actionbar,但是比起它更加的灵活。

设置主题:在style.xml文件中

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

 

MainActivity_Xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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="com.example.materialtest_demo.MainActivity">
    
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:id="@+id/toolbar"
        android:background="?attr/colorPrimary"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:theme="@style/ThemeOverlay.AppCompat.Light">
    </android.support.v7.widget.Toolbar>
</FrameLayout>

接着在清单文件中加入lable标签:

<activity android:name=".MainActivity" android:label="Fruits">

在menu文件包中创建toolbar.xml,为了增加内容,准备三张icon

<?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/backup"
        android:icon="@mipmap/return_item"
        android:title="Backup"
        app:showAsAction="always">
    </item>
    <item
        android:id="@+id/delete"
        android:title="Delete"
        android:icon="@mipmap/delete_1"
        app:showAsAction="ifRoom">
    </item>

<item
        android:id="@+id/setting"
        android:title="Settings"
        android:icon="@mipmap/set_1"
        app:showAsAction="never">
    </item>
</menu>

最后在MainActivity.class

package com.example.materialtest_demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }

@Override
    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.backup:
                Toast.makeText(this," you clicked Backup",Toast.LENGTH_SHORT).show();
                break;
            case R.id.delete:
                Toast.makeText(this,"you clicked Delete",Toast.LENGTH_SHORT).show();
                break;
            case R.id.setting:
                Toast.makeText(this,"you clicked Setting",Toast.LENGTH_SHORT).show();
                break;
            default:
        }
        return true;
    }
}

运行效果

Android——MaterialDesign之一Toolbar

最后,当然Toolbar的功能远远不止这些,需要和其他配合使用。