Material Design 是由 Google 推出的全新的设计语言,谷歌希望它能够为手机、平板电脑、台式机和“其他平台”提供更一致、更广泛的“外观和感觉”。
Design Support 库封装了 Material Design 中最具代表性的一些控件和效果,我们可以利用该库实现酷炫的 Material Design 设计。
让我们从 Toolbar(工具栏)开始说起吧O(∩_∩)O哈哈~
每个活动顶部默认的标题栏是 ActionBar,它只能在活动顶部,所以官方现在推荐直接使用 Toolbar 啦。
1 引入 Toolbar
项目的界面主题定义在 AndroidManifest.xml 的 android:theme
中:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
@style/AppTheme
定义在 res/values/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>
主题默认为 DarkActionBar,即深色主题的 ActionBar。
使用 Toolbar 之前,需要选定没有 ActionBar 的主题,可选主题如下:
主题 | 说明 |
---|---|
Theme.AppCompat.Light.NoActionBar |
主题色调浅色。 |
Theme.AppCompat.NoActionBar |
主题色调深色。 |
这里我们选定 Theme.AppCompat.Light.NoActionBar
。
style 中的 item 代表以下各个部分的主题颜色设置:

布局文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
/>
</FrameLayout>
这里我们把控件的高度被设定为 actionBar 的高度。
活动类:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
}
}
这里把 Toolbar 传入 setSupportActionBar 方法。
运行:

2 自定义标题
修改 AndroidManifest.xml,在主活动类配置中加入 android:label
:
<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 中定义的 label 内容。
效果:

怎么文字变成黑色啦……那么因是之前我们把主题改为淡色系,所以 Toolbar 的主色调也是淡色,那上面的各种元素就会自然采用深色系,纳尼……
所以我们必须在之前的布局文件中,在 Toolbar 中明确指定深色系:
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
效果:

现在好多啦 O(∩_∩)O哈哈~
3 新增按钮
我们准备两张按钮图片,一张用于“新增”,另一张用于“设置”,放在 res/drawable 下:

在 res 下新建一个 menu 目录:

接着创建 toolbar.xml,定义工具栏按钮项:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:material="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add"
android:icon="@drawable/add"
android:title="新增"
material:showAsAction="always" />
<item
android:id="@+id/set"
android:icon="@drawable/set"
android:title="设置"
material:showAsAction="ifRoom" />
</menu>
- 在此,我们定义了新的命名空间
xmlns:material
,这是因为 Material 是 Android 5.0+ 新加入的特性。
2.showAsAction 表示展示方式:
展示方式|说明
always|总是显示。
ifRoom|如果空间足够,则显示。
never|不显示。
修改活动类代码:
@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.add:
Toast.makeText(this, "点击了新增按钮", Toast.LENGTH_SHORT).show();
break;
case R.id.set:
Toast.makeText(this, "点击了设置按钮", Toast.LENGTH_SHORT).show();
break;
}
return true;
}
首先在 onCreateOptionsMenu 中加载菜单配置文件,然后在 onOptionsItemSelected 中处理菜单按钮点击事件。
效果:

网友评论