Android中ActionBar以及menu的代碼設(shè)置樣式
更新時(shí)間:2015年07月16日 12:08:16 投稿:hebedich
這篇文章主要介紹了Android中ActionBar以及menu的代碼設(shè)置樣式的相關(guān)資料,需要的朋友可以參考下
menu部分xml代碼
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/action_search"
android:title="搜索1"
android:orderInCategory="100"
android:showAsAction="always"/>
<item android:id="@+id/action_search2"
android:actionViewClass="android.widget.SearchView"
android:showAsAction="ifRoom|collapseActionView"
android:orderInCategory="100"
android:title="搜索2"/>
<item android:id="@+id/action_share"
android:title="分享"
android:orderInCategory="100"
android:icon="@drawable/ic_action_favor_normal"
android:showAsAction="never"/>
<item android:id="@+id/action_collection"
android:title="收藏"
android:orderInCategory="100"
android:showAsAction="never"/>
<item android:id="@+id/action_font"
android:title="字體大小"
android:orderInCategory="100"
android:showAsAction="never"/>
</menu>
Menu中overflower菜單圖標(biāo)顯示實(shí)現(xiàn)【重寫onMenuOpened方法,使用反射原理】
/**
* 顯示overflower菜單圖標(biāo)
*/
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
if (featureId == Window.FEATURE_ACTION_BAR && menu != null) {
if (menu.getClass().getSimpleName().equals("MenuBuilder")) {
try {
Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
m.setAccessible(true);
m.invoke(menu, true);
} catch (Exception e) {
}
}
}
return super.onMenuOpened(featureId, menu);
}
針對Menu菜單中選項(xiàng)的事件監(jiān)聽操作
/**
* menu菜單點(diǎn)擊操作的監(jiān)聽事件
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case android.R.id.home:
//finish();
super.onBackPressed();
break;
case R.id.action_add:
Toast.makeText(this, "添加", Toast.LENGTH_SHORT).show();
break;
}
return super.onOptionsItemSelected(item);
}
自定義ActionBar
/**
* 初始化 ActionBar內(nèi)容
* */
private ActionBar actionBar;
private void initActionBar(){
actionBar=super.getActionBar();
actionBar.show();
//顯示Home區(qū)域
actionBar.setDisplayShowHomeEnabled(true);
//設(shè)置home區(qū)域回退按鈕
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.back_move_details_normal);
//不顯示Home區(qū)域標(biāo)題
actionBar.setDisplayShowTitleEnabled(true);//
actionBar.setTitle("新聞");//設(shè)置title
//不顯示Logo圖片
actionBar.setDisplayUseLogoEnabled(false);//
//去除home區(qū)域的Icon圖標(biāo)【將icon顏色設(shè)置為透明】
Drawable colorDrawable=new
ColorDrawable(android.R.color.transparent);
actionBar.setIcon(colorDrawable);
//自定義區(qū)域
actionBar.setDisplayShowCustomEnabled(true);
TextView tvTitle=new TextView(this);//this,當(dāng)前承載的
tvTitle.setText("新聞信息");//tvTitle.setId();
tvTitle.setTextSize(25);
int colorVal=getResources().getColor(R.color.white);
tvTitle.setTextColor(colorVal);//tvTitle.setTextColor(Color.WHITE);
tvTitle.setGravity(Gravity.CENTER);
LayoutParams layoutParams=
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
actionBar.setCustomView(tvTitle,layoutParams);
}
實(shí)現(xiàn)效果:

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
您可能感興趣的文章:
- 靈活使用Android中ActionBar和ViewPager切換頁面
- android中開啟actionbar的兩種方法
- Android自定義ActionBar實(shí)例
- Android動態(tài)修改ToolBar的Menu菜單示例
- Android折疊式Toolbar使用完全解析(CollapsingToolbarLayout)
- Android自定義Toolbar使用方法詳解
- Android下拉列表選項(xiàng)框及指示箭頭動畫
- android中一些特殊字符(如:←↑→↓等箭頭符號)的Unicode碼值
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
相關(guān)文章
Android中通過MediaStore獲取音樂文件信息方法
這篇文章主要介紹了Android中通過MediaStore獲取音樂文件信息方法,本文講解了獲取歌曲的名稱、歌曲的專輯名、歌曲的歌手名、歌曲文件的全路徑、歌曲文件的名稱、歌曲文件的發(fā)行日期等音樂文件信息的方法,需要的朋友可以參考下2015-04-04
flutter showModalBottomSheet常用屬性及說明
這篇文章主要介紹了flutter showModalBottomSheet常用屬性及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框(二)
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)通用驗(yàn)證碼輸入框的第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
淺析Android手機(jī)衛(wèi)士讀取聯(lián)系人
這篇文章主要介紹了淺析Android手機(jī)衛(wèi)士讀取聯(lián)系人的相關(guān)內(nèi)容,通過getContentResolver()方法獲取獲取ContentResolver內(nèi)容解析器對象,對android手機(jī)衛(wèi)士讀取聯(lián)系人相關(guān)知識感興趣的朋友參考下吧2016-04-04

