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()); } } });