Android ToolBar使用入门

刚开始使用toolbar的时候在网上找了很多资料,但是都是全面,这里稍作总结

toolbar的使用还是比较简单,直接在布局文件里面引入就行,代码如下

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"//id别忘了
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/lightskyblue"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"//标题居中  
        android:layout_gravity="center"
        android:textColor="@color/deep_yellow"
        android:textSize="@dimen/textsize_18" />

    <ImageView
        android:id="@+id/iconRight"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_gravity="right"//右边如果有什么按钮直接代码里面设置没有就不设置
        android:layout_marginRight="10dp" />

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

toolbar左边自带点击按钮,如果在一般的activity使用,可以设置为返回按钮,也可以和DrawerLayout配合使用代码分别如下
[1]配合DrawerLayout使用

@BindView(R.id.toolbar)//找到控件
Toolbar toolbar;

 setSupportActionBar(toolbar);//设置为标题栏
 toolbar.setTitle("");//把默认的title去掉,用上面布局里面的title去设置想要的标题
//菜单
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open,    R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();
        toolbar.setNavigationIcon(R.mipmap.ic_menu);//这里是设置打开菜单的按钮图片,如果设置上去图片过大,可以切一个小一点的就行,好像不能手动设置宽高,如果有谁知道麻烦告诉我一下··

[2]在普通activity使用

    title.setText("Tittle");
    getSupportActionBar().setDisplayShowTitleEnabled(false);//设置默认标题不显示
        toolbar.setNavigationIcon(R.mipmap.ic_back_white);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {//直接做返回的点击事件
            @Override
            public void onClick(View v) {
                finish();
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值