ToolBar

先将app的theme改变为noActionBar

XML布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<!-- android:background="?attr/colorPrimary"-->
<android.support.v7.widget.Toolbar
android:id="@+id/myTool"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#99ffcc"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
></android.support.v7.widget.Toolbar>
</FrameLayout>

RES资源
在res文件夹中创建menu文件夹 , 右键创建menu资源文件

<item
android:id="@+id/menu1"
android:icon="@drawable/ic_archive_black_48dp" 图标
android:title="menu1" 标题
app:showAsAction="always"></item> 显示的位置

always 总是显示
ifRoom 屏幕空间不足时就会隐藏
never 从不显示



JAVA
通过setSupportActionBar();设置
加载menu资源文件
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.toolbar, menu);
return true;
}
设置监听
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu1:
Toast.makeText(this, "menu1", Toast.LENGTH_SHORT).show();
break;
case R.id.menu2:
Toast.makeText(this, "menu2", Toast.LENGTH_SHORT).show();
break;
case R.id.menu3:
Toast.makeText(this, "menu3", Toast.LENGTH_SHORT).show();
break;
}
return true;
}




actionBar = getSupportActionBar();
if(actionBar !=null){
actionBar.setDisplayHomeAsUpEnabled(true); //默认为小箭头 id为android.R.id.home
actionBar.setHomeAsUpIndicator(R.drawable.ic_visibility_black_48dp);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值