新手一枚汰瘫,請多指教????????????
(一)Toolbar 加載menu
創(chuàng)建Toolbar
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
獲取組件并加載菜單
private void initView() {
//獲取組件
mToolbar = (Toolbar) findViewById(R.id.toolbar);
//設(shè)置主標(biāo)題
mToolbar.setTitle("這是主標(biāo)題");
//設(shè)置副標(biāo)題
mToolbar.setSubtitle("這是副標(biāo)題");
//設(shè)置主標(biāo)題顏色
mToolbar.setTitleTextColor(Color.WHITE);
//設(shè)置副標(biāo)題顏色
mToolbar.setSubtitleTextColor(Color.WHITE);
//加載菜單
mToolbar.inflateMenu(R.menu.menu);
//點(diǎn)擊事件
mToolbar.setOnMenuItemClickListener(this);
//overflower圖標(biāo)
mToolbar.setOverflowIcon(getDrawable(R.drawable.ic_more));
}
創(chuàng)建一個(gè)menu文件
設(shè)置幾個(gè)item
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:title="收藏"
android:icon="@drawable/ic_bookmark"
android:id="@+id/collect"
/>
<item
android:title="刪除"
android:icon="@drawable/ic_delete"
android:id="@+id/delete"
/>
運(yùn)行效果
點(diǎn)擊事件
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.collect:
showToast("收藏");
break;
case R.id.delete:
showToast("刪除");
break;
}
return false;
}
/**
* Toast 方法
* @param s
*/
private void showToast(String s) {
Snackbar.make(mToolbar,s,Snackbar.LENGTH_SHORT).show();
}
運(yùn)行效果
(二)ActionMenuView 使用
創(chuàng)建 ActionMenuView
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:background="@color/colorPrimary"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.ActionMenuView
android:id="@+id/action_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v7.widget.Toolbar>
獲取組件
private void initView() {
//獲取組件
mActionView = (ActionMenuView) findViewById(R.id.action_view);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
//設(shè)置點(diǎn)擊事件
mActionView.setOnMenuItemClickListener(this);
//設(shè)置成為 actinobar
setSupportActionBar(mToolbar);
//不顯示標(biāo)題
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
創(chuàng)建一個(gè)menu文件
設(shè)置幾個(gè)item
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
app:showAsAction="always"
android:title="收藏"
android:icon="@drawable/ic_bookmark"
android:id="@+id/collect"
/>
<item
app:showAsAction="always"
android:title="刪除"
android:icon="@drawable/ic_delete"
android:id="@+id/delete"
/>
<item
android:id="@+id/help"
app:showAsAction="always"
android:title="幫助"
android:icon="@drawable/ic_live_help"
/>
<item
android:id="@+id/translate"
app:showAsAction="always"
android:title="翻譯"
android:icon="@drawable/ic_translate"
/>
<item
android:id="@+id/bluetooth"
app:showAsAction="always"
android:title="藍(lán)牙"
android:icon="@drawable/ic_bluetooth"
/>
</menu>
設(shè)置menu
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu,mActionView.getMenu());
return super.onCreateOptionsMenu(menu);
}
運(yùn)行效果:
點(diǎn)擊事件:
//實(shí)現(xiàn)此方法
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.collect:
showToast("收藏");
break;
case R.id.delete:
showToast("刪除");
break;
case R.id.help:
showToast("幫助");
break;
case R.id.translate:
showToast("翻譯");
break;
case R.id.bluetooth:
showToast("藍(lán)牙");
break;
}
return false;
}
//Toast
private void showToast(String s) {
Snackbar.make(mToolbar,s,Snackbar.LENGTH_SHORT).show();
}
點(diǎn)擊效果:
ps:一點(diǎn)小技巧
//這樣使用可以設(shè)置overflower圖標(biāo)
mToolbar.inflateMenu(R.menu.menu);
mToolbar.setOverflowIcon(getDrawable(R.drawable.ic_more));