Android控件Toolbar的使用

Toolbar
Toolbar的功能比ActionBar强大(并没有多少乱用),多搭配侧滑DrawerLayout一起使用。
Frist:在gradle文件中添加appcompat_v7包的引用,具体配置如下:

compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.4.0'

Second:Style中更改默认的主题为:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">//不使用系统的Actionbar
 -----------------------可选配置开始---------------------------
         <!--导航栏底色-->
        <item name="colorPrimary">@color/accent_material_dark</item>
        <!--状态栏底色-->
        <item name="colorPrimaryDark">@color/accent_material_light</item>
        <!--导航栏上的标题颜色-->
        <item name="android:textColorPrimary">@android:color/black</item>
        <!--Activity窗口的颜色-->
        <item name="android:windowBackground">@color/material_blue_grey_800</item>
        <!--按钮选中或者点击获得焦点后的颜色-->
        <item name="colorAccent">#00ff00</item>
        <!--和 colorAccent相反,正常状态下按钮的颜色-->
        <item name="colorControlNormal">#ff0000</item>
        <!--Button按钮正常状态颜色-->
        <item name="colorButtonNormal">@color/accent_material_light</item>
        <!--EditText 输入框中字体的颜色-->
        <item name="editTextColor">@android:color/white</item>
 ---------------------可选配置结束----------------------------
    </style>

Third:在layout布局文件中引用 android.support.v7.widget.Toolbar 代码如下:

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_height="?attr/actionBarSize"
        android:layout_width="match_parent"
        <!--Toolbarshadows,可选配置-->
        android:elevation="2dp"
        <!--设置Toolbar的背景色-->
        android:background="?attr/colorPrimary" >
    </android.support.v7.widget.Toolbar>

Fourth:在XXXActivity中显示就可以了,代码如下

    private Toolbar mToolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mToolbar= (Toolbar) findViewById(R.id.toolbar);
        //设置logo图片
        mToolbar.setLogo(R.mipmap.ic_launcher);
        //设置标题
        mToolbar.setTitle("Material Desgin");
        //设置副标题
        mToolbar.setSubtitle("test test");
        setSupportActionBar(mToolbar);
        //设置NavigationIcon的图片
        mToolbar.setNavigationIcon(R.drawable.android);
        //设置菜单的监听
        mToolbar.setOnMenuItemClickListener(MyOnMenuItemClick);
    }

我的toolbar的效果图:
至于我的Toolbar为什么是圆角的,我的外部布局是CardView而已
至于我的Toolbar为什么是圆角的,我的外部布局是CardView而已
更多使用详情请参考:
http://blog.youkuaiyun.com/feiduclear_up/article/details/46457433
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2014/1118/2006.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值