Material Design之ToolBar

来源:互联网 发布:淘宝存在隐形降权吗 编辑:程序博客网 时间:2024/05/21 04:44

1.ToolBar:

每个活动的最顶部标题栏就是ActionBar,现在很少用ActionBar了,ToolBar继承自ActionBar,可以配合其他控件来完成Material Design的效果。

新建的一个项目默认都是显示ActionBar,而这个ActionBar是来自于主题中,我们不断地在AndroidManifeast.xml中追查theme属性即可找到

Theme.AppCompat.Light.DarkActionBar这个中带有ActionBar,现在我们要指定一个不带ActionBar的主题,通常有Theme.Appcompat.NoActionBar和Theme.
Appcompat.Light.NoActionBar这两种主题可选。Theme.Appcompat.NoActionBar为深色主题,它会将界面的主题颜色设为深色,陪衬颜色变为淡色;而Theme.
Appcompat.Light.NoActionBar表示淡色主题,他会将界面的主题颜色变为淡色,陪衬颜色变为深色。
    在AndroidManifeast.xml中theme找到style.xml,将其parent修改为不断ActionBar的,则style.xml文件如下:
<resources>    <!-- 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></resources>
    各个属性位置如下:
    
修改activity_main.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"<!--用xmls:app指定了一个新的命名空间,正式由于用它指定了一个命名空间,才能
使用android:id,android.layout_width等属性。-->    android:layout_width="match_parent"    android:layout_height="match_parent">    <android.support.v7.widget.Toolbar<!--由于去掉了Actionbar,所以要自己添加-->        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.v7.widget.Toolbar></FrameLayout>
我们刚才在style,xml中将程序的主题指定成了淡色主题,因此现在ToolBar也是淡色主题,而为了和主题颜色区分开ToolBar上面的各种元素就会自动使用深色,但是
系统的这种区分效果非常差,之前ActionBar的文字都是白色的,现在变为黑色很难看。为了能让ToolBar单独使用深色主题,所以使用了theme属性。然而这样的话如果
ToolBar中有弹出操蛋,那么弹出菜单项就会也为深色,而非常难看,所以使用了app:popTheme属性单独将弹出菜单项指定为淡色。
    在MainActivity.java中:
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);    }}
运行之后发现和ActionBar一样,但是它已经是ToolBar而不是ActionBar,具有Material Design效果的能力。下面为ToolBar增加一些内容:
    修改标题栏上的文字AndroidManifest.xml:
<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">    <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>
给avtivity增加了一个label属性,没有该属性则显示的是application的label内容即应用的名字。
    新建标题栏按钮:
    新建一个menu resource file:toolbar.xml:
<?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="@drawable/ic_backup"        android:title="Backup"        app:showAsAction="always"/><!--always永远显示,如果屏幕空间不够才不显示,否则也不显示在菜单中-->    <item        android:id="@+id/delete"        android:icon="@drawable/ic_delete"        android:title="Backup"        app:showAsAction="ifRoom"/><!--ifroom表示如果空间够的话才显示,否则在菜单中-->    <item        android:id="@+id/settings"        android:icon="@drawable/ic_settings"        android:title="Settings"        app:showAsAction="never"/><!--表示永远显示在菜单中--></menu>
之后再MainActivity.java中加入下面代码:
public boolean onCreateOptionsMenu(Menu menu){    getMenuInflater().inflate(R.menu.toolbar,menu);    return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){    switch (item.getItemId()){        case R.id.backup:            Toast.makeText(this, "Backup", Toast.LENGTH_SHORT).show();            break;        case R.id.delete:            Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();            break;        case R.id.settings:            Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();            break;        default:    }    return true;}

原创粉丝点击