先看个不通过Toolbar自带的菜单项设置Toolbar。
Toolbar布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/appBar_height"
android:background="?attr/colorPrimary">
<TextView
android:id="@+id/tv_toolbar"
android:layout_gravity="center"
android:textColor="@color/txt_white"
android:textSize="@dimen/appbar_text"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnRight"
android:layout_gravity="right"
android:textColor="@color/txt_white"
android:textSize="@dimen/appbar_Right_text"
android:textStyle="bold"
style="@style/ActionButton.Overflow"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/toolbar"
android:orientation="vertical" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
标题栏和按钮都是自己添加上的,标题居中,右边按钮默认不显示。
private void initToolbar(){
mToolbar = (Toolbar) findViewById(R.id.toolbar);
tvTitle = (TextView) mToolbar.findViewById(R.id.tv_toolbar);
btnRight = (Button) mToolbar.findViewById(R.id.btnRight);
setSupportActionBar(mToolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowTitleEnabled(false);
}
getSupportActionBar().setHomeButtonEnabled(true);//设置返回键可用,如果某个页面想隐藏掉返回键比如首页,可以调用mToolbar.setNavigationIcion(null);
getSupportActionBar().setDisplayShowHomeEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
拿到标题栏和按钮就可以设置标题栏和右边的按钮了
protected void setTitleName(String name){
tvTitle.setText(name);
}
protected void setToolbarRight(String text, @Nullable Integer icon, View.OnClickListener btnClick){
if(text!=null)
{
btnRight.setText(text);
}
if (icon != null) {
btnRight.setBackgroundResource(icon.intValue());
ViewGroup.LayoutParams linearParams = btnRight.getLayoutParams();
linearParams.height= ToolUtils.dp2px(this,26);
linearParams.width=ToolUtils.dp2px(this,26);
btnRight.setLayoutParams(linearParams);
}
btnRight.setOnClickListener(btnClick);
}
调用的时候
setToolbarRight("提交", null, new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowUtils.showToast("提交操作");
}
});
显示效果如下:
或者把按钮设置成图片:
setToolbarRight(null, R.drawable.icon_plus_menu, new View.OnClickListener() {
@Override
public void onClick(View v) {
ShowUtils.showToast("提交操作");
}
});
效果如下:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_scan" android:title="more"
android:icon="@drawable/icon_scan"
android:orderInCategory="100" app:showAsAction="always" />
<item android:id="@+id/action_add" android:title="more"
android:icon="@drawable/icon_plus_menu"
android:orderInCategory="100" app:showAsAction="always" />
<item
android:id="@+id/action_item1"
android:title="按钮1"
app:showAsAction="never" />
<item
android:id="@+id/action_item2"
android:title="按钮2"
app:showAsAction="never" />
</menu>
showAsAction=“ifRoom” 表示有空间就显示出来,没有就不显示,never是不会显示在Toolbar上,还有always 是显示在Toolbar上。如果不显示就会以三个点的样式,点击三个点显示菜单。
然后再Activity里重写如下两个方法:添加menu,设置menu点击按钮
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.<span style="color:#ff0000;">menu.toolbar_menu</span>, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_scan:
ShowUtils.showToast("点击了扫描");
break;
case R.id.action_add:
ShowUtils.showToast("点击了添加");
break;
case R.id.action_item1:
ShowUtils.showToast("点击了菜单1");
break;
case R.id.action_item2:
ShowUtils.showToast("点击了菜单2");
break;
default:break;
}
return true;
}
点击三个点图标,显示隐藏的菜单项,效果如下:暴丑
修改,在样式文件下添加如下样式:
<!-- Toolbar主题设置-->
<style name="ToolBarTheme" parent="@style/ThemeOverlay.AppCompat.Dark">
<item name="android:textColorSecondary">#ffffff</item>
<item name="android:colorBackground">#aa333333</item>
<item name="actionOverflowMenuStyle">@style/OverflowMenuStyle</item> <!--新增一个item,用于控制menu-->
<!-- <item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle</item>-->
</style>
<!--修改toolbar弹出菜单的位置-->
<style name="OverflowMenuStyle" parent="Widget.AppCompat.Light.PopupMenu.Overflow">
<item name="overlapAnchor">false</item> <!--把该属性改为false即可使menu位置位于toolbar之下-->
</style>
<!--修改toolbar菜单按钮的style-->
<style name="OverflowButtonStyle" parent="@android:style/Widget.ActionButton.Overflow">
<item name="android:src">@drawable/ico_fail</item>
</style>-->
<!--toolbar菜单按钮的颜色-->
<style name="ActionButton.Overflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
</style>
如果想把三个点换成其他图片:就将注释的那个item 添加上
<item name="android:actionOverflowButtonStyle">@style/OverflowButtonStyle
也可以通过代码把三个点点换成其他图片:
mToolbar.setOverflowIcon(BaseUtils.getResImg(com.wyyy.corelibrary.R.drawable.ico_success));
完成后,在Toolbar布局文件中添加如下红色的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
<span style="color:#ff0000;"> <strong> xmlns:toolbar="http://schemas.android.com/apk/res-auto"</strong></span>
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="@dimen/appBar_height"
android:background="?attr/colorPrimary"
<span style="color:#ff0000;"> <strong>toolbar:theme="@style/ToolBarTheme"</strong></span>>
<TextView
android:id="@+id/tv_toolbar"
android:layout_gravity="center"
android:textColor="@color/txt_white"
android:textSize="@dimen/appbar_text"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btnRight"
android:layout_gravity="right"
android:textColor="@color/txt_white"
android:textSize="@dimen/appbar_Right_text"
android:textStyle="bold"
style="@style/ActionButton.Overflow"
android:layout_marginRight="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/toolbar"
android:orientation="vertical" />
<android.support.design.widget.CoordinatorLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
最终效果如下: