Material Design之使用Toolbar

来源:互联网 发布:科比生涯平均数据 编辑:程序博客网 时间:2024/06/05 08:21

效果图如下:
这里写图片描述

activity_toollbar.xml如下:

<?xml version="1.0" encoding="utf-8"?><!--要添加xmlns:app="http://schemas.android.com/apk/res-auto"属性,否则不能使用app:attribute--><android.support.design.widget.CoordinatorLayout    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"    android:fitsSystemWindows="true"    tools:context="com.example.administrator.material.ToollbarActivity">    <!--与ActionBar不能一起使用,否则会报错This Activity already has an action bar supplied by the window decor-->    <!-- android:background="?attr/colorPrimary" 使用colorPrimary定义的颜色 这个当然可以自己定义-->    <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.CoordinatorLayout>

Activity代码如下:

public class ToollbarActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_toollbar);        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        setSupportActionBar(toolbar);//支持ActionBar    }    @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.delete:                Toast.makeText(ToollbarActivity.this, "你点击了删除", Toast.LENGTH_LONG).show();                break;            case R.id.share:                Toast.makeText(ToollbarActivity.this, "你点击了分享", Toast.LENGTH_LONG).show();                break;            case R.id.setting:                Toast.makeText(ToollbarActivity.this, "你点击了分享", Toast.LENGTH_LONG).show();                break;        }        return true;    }}

menu文件夹中的toolbar.xml

<?xml version="1.0" encoding="utf-8"?><!--通过ToollbarActivity中的onCreateOptionsMenu和onOptionsItemSelected方法控制--><menu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:app="http://schemas.android.com/apk/res-auto">    <!--awlays表示永远显示在toolbar中,如果屏幕控件不够,就不显示-->    <!--ifRoom表示如果屏幕空间足够,就显示在屏幕中,如果不够,就显示在菜单中-->    <!--never表示永远显示在菜单中-->    <!--Toolbar的action按钮只会显示图标,菜单中的只会显示文字内容-->    <item        android:id="@+id/delete"        android:icon="@mipmap/delete"        android:title="delete"        app:showAsAction="always" />    <item        android:id="@+id/setting"        android:icon="@mipmap/setting"        android:title="setting"        app:showAsAction="ifRoom" />    <item        android:id="@+id/share"        android:icon="@mipmap/share"        android:title="share"        app:showAsAction="never" /></menu>
0 0