android toolbar的使用

1.注意:请记得用 support v7 里的 toolbar,不然然只有 API Level 21 也就是 Android 5.0 以上的版本才能使用。

2.xml

注意:如果执行事项使用左边的titletoolbar里面的布局可以不写

<android.support.v7.widget.Toolbar
    android:id="@+id/favorite_toolbar"
    style="@style/Widget.AppCompat.Toolbar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/height_top_bar"
    android:gravity="center"
    android:background="@color/maincolor"
    android:minHeight="@dimen/height_top_bar"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:focusable="true"
        android:focusableInTouchMode="true">
        <TextView
            android:id="@+id/action_contact_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:textSize="18sp"
            android:textColor="@color/white"
            android:text="我的收藏" />
        <Button
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:id="@+id/btn_edit"
            android:background="@null"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="编辑"
            android:textColor="@color/white"
            android:textSize="18sp"/>
    </RelativeLayout>
</android.support.v7.widget.Toolbar>


3.使用

Toolbar toolbar = (Toolbar) findViewById(R.id.favorite_toolbar);
toolbar.setTitle("我的收藏");
toolbar.setTitleTextColor(getResources().getColor(R.color.white));
setSupportActionBar(toolbar);
toolbar.setNavigationIcon(R.drawable.back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

4.里面的点击事件也是可以得到的例编辑按钮

@ViewById(R.id.btn_edit)
public Button btn_edit;

btn_edit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        if (BamasosoApplication.getInstance().isEdit_fav()){
            //真的话显示
            BamasosoApplication.getInstance().setEdit_fav(false);
            btn_edit.setText("编辑");
            EventCenter.getInstance().post(new FavEvent());
        }else{
            //fakse的话不显示
            BamasosoApplication.getInstance().setEdit_fav(true);
            btn_edit.setText("取消");
            EventCenter.getInstance().post(new FavEvent());
        }

    }
});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值