自定義菜單ContextMenu
CntextMenu依賴第三方庫,首先需要在build下添加依賴
第一步,爲工程添加依賴
compile 'com.yalantis:contextmenu:1.0.6'
第二步,研究發現,這個菜單隻能限定有fragment上,activity上不適合添加此菜單
因此在activity中添加了一個toolbar和一個fragment
第三步,創建菜單
3.1初始化toolbar
講工程的主題設置繼承爲Notoolbar,纔可以重新新建一個toolbar
下面的代碼爲添加toolbar
private void initToolbar(){
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
TextView textView = (TextView) findViewById(R.id.text_view_toolbar_title);
setSupportActionBar(toolbar);//toolbar在開始的時候已經初始化
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar.setNavigationIcon(R.mipmap.btn_back);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
textView.setText("Samantha");
// toolbar.setOnMenuItemClickListener(this);
}
3.2創建menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/context_menu"
android:title="@string/context_menu"
android:icon="@mipmap/btn_add"
android:orderInCategory="100"
app:showAsAction="always" />
</menu>
創建完自定義菜單後需要重寫activity裏面的onCreateOptionsMenu方法,將菜單顯示出來
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
return true;
}
第四步、創建fragment,將菜單選項加入到fragment裏面
4.1創建一個ContextMenuDialogFragment,這個類是第三方源代碼
4.2 創建菜單列表
private List<MenuObject> getMenuObjects() {
// You can use any [resource, bitmap, drawable, color] as image:
// item.setResource(...)
// item.setBitmap(...)
// item.setDrawable(...)
// item.setColor(...)
// You can set image ScaleType:
// item.setScaleType(ScaleType.FIT_XY)
// You can use any [resource, drawable, color] as background:
// item.setBgResource(...)
// item.setBgDrawable(...)
// item.setBgColor(...)
// You can use any [color] as text color:
// item.setTextColor(...)
// You can set any [color] as divider color:
// item.setDividerColor(...)
List<MenuObject> menuObjects = new ArrayList<>();
MenuObject close = new MenuObject();
close.setResource(R.mipmap.icn_close);
MenuObject send = new MenuObject("Send message");
send.setResource(R.mipmap.icn_1);
MenuObject like = new MenuObject("Like profile");
Bitmap b = BitmapFactory.decodeResource(getResources(), R.mipmap.icn_2);
like.setBitmap(b);
MenuObject addFr = new MenuObject("Add to friends");
BitmapDrawable bd = new BitmapDrawable(getResources(),
BitmapFactory.decodeResource(getResources(), R.mipmap.icn_3));
addFr.setDrawable(bd);
MenuObject addFav = new MenuObject("Add to favorites");
addFav.setResource(R.mipmap.icn_4);
MenuObject block = new MenuObject("Block user");
block.setResource(R.mipmap.icn_5);
menuObjects.add(close);
menuObjects.add(send);
menuObjects.add(like);
menuObjects.add(addFr);
menuObjects.add(addFav);
menuObjects.add(block);
return menuObjects;
}
3.3創建完菜單列表之後把菜單列表添加進fragment裏面
private void initMenuFragment(){
MenuParams menuParams = new MenuParams();
menuParams.setActionBarSize((int) getResources().getDimension(R.dimen.tool_bar_height));
menuParams.setMenuObjects(getMenuObjects());//設置菜單項
menuParams.setClosableOutside(false);
fragment = ContextMenuDialogFragment.newInstance(menuParams);
//設置監聽
fragment.setItemClickListener(this);
fragment.setItemLongClickListener(this);
}
第四步、至此,已經完成加載了
接下來只需要重寫activity裏面的 兩個父方法
將菜單按鈕加載出來
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main,menu);
return true;
}
//監聽是否是點擊了菜單按鈕
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.context_menu:
if (fragmentManager.findFragmentByTag(ContextMenuDialogFragment.TAG) == null) {
fragment.show(fragmentManager, ContextMenuDialogFragment.TAG);
}
break;
}
return super.onOptionsItemSelected(item);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.