码迷,mamicode.com
首页 > 移动开发 > 详细

Android --Toolbar的使用

时间:2015-08-26 12:08:42      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

在布局文件中添加如下代码:

 <android.support.v7.widget.Toolbar
        android:id="@+id/toobar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">

 </android.support.v7.widget.Toolbar>

注意:这里使用的是support v7中的Toolbar ,如果直接使用Toolbar,则只用在API level 21 的版本上才有效

然后在主题中添加如下代码:

<!--隐藏Actionbar-->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<!--Toolbar的颜色-->
<item name="colorPrimary">@color/accent_material_light</item>

在Activity文件中添加如下代码:

//使用Toolbar代替Actionbar
Toolbar toolbar = (Toolbar)findViewById(R.id.toobar);
setSupportActionBar(toolbar);

到目前为止Toolbar就已经代替了Actionbar
效果图如下:
技术分享

当然Toolbar中还有很多方法,我在这里列举一部分常用的:

//设置标题
toolbar.setTitle("标题");
//设置子标题
toolbar.setSubtitle("子标题");
//设置Logo
toolbar.setLogo(R.mipmap.ic_launcher);
//设置导航图标
toolbar.setNavigationIcon(R.mipmap.ic_launcher);

效果图:
技术分享

说明:由于我是使用AndroidStudio开发的所以我这里使用的是R.mipmap.ic_lanucher使用Eclipse的同学们可以使用R.drawable.ic_lanucher

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android --Toolbar的使用

标签:

原文地址:http://blog.csdn.net/tuu_zed/article/details/48000221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!