Android5.x新特性之 Toolbar和Theme的使用

Toolbar

你还在为Android 的ActionBar的文字不能随意设置位置而烦恼么?你还在为ActionBar不能自定义添加自己的布局而烦恼么?现在告诉你一个好消息,当你看到这篇文章时,就不必烦恼了。Google在Android5.0以后推出了一个Toolbar来完全代替之前的Actionbar,Toolbar的出现解决了Actionbar的各种限制,Toolbar可以完全自定义和配置。我们从以下几个点了解Toolbar的使用

  1. Toolbar的基础使用
  2. Toolbar配置主题Theme
  3. Toolbar中常用的控件设置
  4. Toolbar的自定义

Toolbar的基础使用

我们从以下几点来一步一步的学习Toolbar的使用

  1. Style(风格)
  2. Layout(布局)
  3. Activity(代码)

Style

为了能在你的Activity中使用Toolbar,你必须在工程里修改styles.xml文件里的主题风格,系统默认如下

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

这种Theme表示使用系统之前的ActionBar,那么我们想要使用Toolbar怎么办呢?

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

这个主题表示不使用系统的Actionbar了,这是第一步。

Layout布局

为了使用Toolbar,我们第二步是在你的activity_main.xml布局里添加Support v7提供的Toolbar,代码如下

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity">
  6. <android.support.v7.widget.Toolbar
  7. android:id="@+id/toolbar"
  8. android:layout_width="match_parent"
  9. android:layout_height="?attr/actionBarSize"
  10. android:minHeight="?attr/actionBarSize"
  11. >
  12. </android.support.v7.widget.Toolbar>
  13. </RelativeLayout>

为了在你的UI中使用Toolbar,你得为每个activity布局添加Toolbar,并且给Toolbar设置一个id android:id=”@+id/toolbar”。这是第二部。

代码添加Toolbar

为了使用Toolbar,我们第三部是在你的Activity代码中做如下代码处理:

  1. Toolbar toolbar;
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. toolbar = findView(R.id.toolbar);
  7. setSupportActionBar(toolbar);
  8. }

代码中通过findView找到Toolbar,然后通过setSupportActionBar(toolbar);将Toolbar设置为Activity的导航栏。

通过上面的三个步骤,你就已经使用了Support v7提供的Toolbar了。看看那效果图。

Android5.x新特性之 Toolbar和Theme的使用 
是不是感觉很丑?没有一点MD设计的风格,不过别失望,这仅仅是为了让Toolbar正常工作而已,为了让Toolbar有Material Design风格,我们必须去设置Toolbar的主题风格。

Toolbar配置主题Theme

我们重新配置系统主题Theme,修改styles.xml代码如下:

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. <!-- Customize your theme here. -->
  3. <!--导航栏底色-->
  4. <item name="colorPrimary">@color/accent_material_dark</item>
  5. <!--状态栏底色-->
  6. <item name="colorPrimaryDark">@color/accent_material_light</item>
  7. <!--导航栏上的标题颜色-->
  8. <item name="android:textColorPrimary">@android:color/black</item>
  9. <!--Activity窗口的颜色-->
  10. <item name="android:windowBackground">@color/material_blue_grey_800</item>
  11. <!--按钮选中或者点击获得焦点后的颜色-->
  12. <item name="colorAccent">#00ff00</item>
  13. <!--和 colorAccent相反,正常状态下按钮的颜色-->
  14. <item name="colorControlNormal">#ff0000</item>
  15. <!--Button按钮正常状态颜色-->
  16. <item name="colorButtonNormal">@color/accent_material_light</item>
  17. <!--EditText 输入框中字体的颜色-->
  18. <item name="editTextColor">@android:color/white</item>
  19. </style>

各个属性就不解释了,注释都很清楚。我们来看看Toolbar怎么使用这些主题吧?

配置activity_main.xml中的Toolbar改成为如下:

  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="?attr/colorPrimary"
  6. >
  7. </android.support.v7.widget.Toolbar>

相比上面的Toolbar配置,这里只多添加了 这么一行代码

  1. android:background="?attr/colorPrimary"

给Toolbar设置背景属性,这里使用了styles.xml文件中如下属性

  1. <!--导航栏底色-->
  2. <item name="colorPrimary">@color/accent_material_dark</item>

经过如下配置再来看看效果图吧!

Android5.x新特性之 Toolbar和Theme的使用

效果有点改进,我们继续发现Toolbar的优势吧!

Toolbar中常用的控件设置

那么Toolbar是否都有Actionbar的所有功能呢?毋庸置疑,来看代码:

  1. toolbar = findView(R.id.toolbar);
  2. setSupportActionBar(toolbar);
  3. getSupportActionBar().setDisplayShowTitleEnabled(false);
  4. toolbar.setTitle("主标题");
  5. toolbar.setSubtitle("副标题");
  6. toolbar.setLogo(R.drawable.ic_launcher);
  7. toolbar.setNavigationIcon(android.R.drawable.ic_input_delete);

Toolbar可以设置 Title(主标题),Subtitle(副标题),Logo(logo图标)NavigationIcon(导航按钮)。

注意 如果你想要通过toolbar.setTitle(“主标题”);设置Toolbar的标题,你必须在调用它之前调用如下代码:

  1. getSupportActionBar().setDisplayShowTitleEnabled(false);

上面代码用来隐藏系统默认的Title。

那么Toolbar能不能使用Menu菜单功能呢?答案是肯定的了。来看看加载如下menu菜单的Toolbar吧

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:app="http://schemas.android.com/apk/res-auto"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. tools:context=".MainActivity">
  5. <item
  6. android:id="@+id/action_edit"
  7. android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
  8. android:orderInCategory="80"
  9. android:title="查找"
  10. app:showAsAction="always" />
  11. <item
  12. android:id="@+id/action_share"
  13. android:icon="@drawable/abc_ic_menu_share_mtrl_alpha"
  14. android:orderInCategory="90"
  15. android:title="分享"
  16. app:showAsAction="always" />
  17. <item
  18. android:id="@+id/action_settings"
  19. android:orderInCategory="100"
  20. android:title="@string/action_settings"
  21. app:showAsAction="never" />
  22. </menu>

怎么给menu的各个Item添加点击事件呢?Toolbar给我们提供如下方法

Activity继承Toolbar的OnMenuItemClickListener接口

  1. public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener
  1. //实现接口
  2. toolbar.setOnMenuItemClickListener(this);
  3. @Override
  4. public boolean onMenuItemClick(MenuItem item) {
  5. switch (item.getItemId()) {
  6. case R.id.action_edit:
  7. Toast.makeText(this, "查找按钮", Toast.LENGTH_SHORT).show();
  8. break;
  9. case R.id.action_share:
  10. Toast.makeText(this, "分享按钮", Toast.LENGTH_SHORT).show();
  11. break;
  12. }
  13. return false;
  14. }

至此,Toolbar添加控件就基本完结了,来看看效果如下

Android5.x新特性之 Toolbar和Theme的使用

是不是很炫?我们还没有使用自定义的Toolbar呢?那怎么使用呢?

Toolbar的自定义

其实Toolbar是继承ViewGroup的一个容器控件,言外之意就是我们可以在Toolbar添加自己的布局了。看代码

  1. <android.support.v7.widget.Toolbar
  2. android:id="@+id/toolbar"
  3. android:layout_width="match_parent"
  4. android:layout_height="?attr/actionBarSize"
  5. android:background="?attr/colorPrimary">
  6. <RelativeLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:gravity="center">
  10. <Button
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:text="add" />
  14. <TextView
  15. android:layout_width="wrap_content"
  16. android:layout_height="wrap_content"
  17. android:layout_centerInParent="true"
  18. android:layout_gravity="center_vertical"
  19. android:text="标题"
  20. android:textSize="18sp" />
  21. <ImageView
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_alignParentRight="true"
  25. android:background="@drawable/abc_ic_menu_share_mtrl_alpha" />
  26. </RelativeLayout>
  27. </android.support.v7.widget.Toolbar>

效果图: 
Android5.x新特性之 Toolbar和Theme的使用

这样我们就可以任意给Toolbar布局了。也解决了标题不能居中的问题。有特殊需求的Toolbar的童鞋就可以自行补脑实现各种需求效果啦!

Android5.x Material Design 主题风格Theme配置

在通往Material Design风格的路上总是遥远的,但也阻挡不了我们学习的劲头,仅仅会使用Toolbar是不够的。除了Toolbar的风格,我们还可以通过设置Theme主题该控制Android很多控件的风格。直接上一张图片效果。

Android5.x新特性之 Toolbar和Theme的使用

以上效果的主题配置如下:

  1. <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
  2. <!-- Customize your theme here. -->
  3. <!--导航栏底色-->
  4. <item name="colorPrimary">@color/accent_material_dark</item>
  5. <!--状态栏底色-->
  6. <item name="colorPrimaryDark">@color/accent_material_light</item>
  7. <!--导航栏上的标题颜色-->
  8. <item name="android:textColorPrimary">@android:color/black</item>
  9. <!--Activity窗口的颜色-->
  10. <item name="android:windowBackground">@color/material_blue_grey_800</item>
  11. <!--按钮选中或者点击获得焦点后的颜色-->
  12. <item name="colorAccent">#00ff00</item>
  13. <!--和 colorAccent相反,正常状态下按钮的颜色-->
  14. <item name="colorControlNormal">#ff0000</item>
  15. <!--Button按钮正常状态颜色-->
  16. <item name="colorButtonNormal">@color/accent_material_light</item>
  17. <!--EditText 输入框中字体的颜色-->
  18. <item name="editTextColor">@android:color/white</item>
  19. </style>

1.colorPrimary: Toolbar导航栏的底色。 
2.colorPrimaryDark:状态栏的底色,注意这里只支持Android5.0以上的手机。 
3.textColorPrimary:整个当前Activity的字体的默认颜色。 
4.android:windowBackground:当前Activity的窗体颜色。 
5.colorAccent:CheckBox,RadioButton,SwitchCompat等控件的点击选中颜色 
6.colorControlNormal:CheckBox,RadioButton,SwitchCompat等默认状态的颜色。 
7.colorButtonNormal:默认状态下Button按钮的颜色。 
8.editTextColor:默认EditView输入框字体的颜色。

上一篇:hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)


下一篇:android 经典下雨效果