menu部分xml代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
<? 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菜单图标显示实现【重写onMenuOpened方法,使用反射原理】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 显示overflower菜单图标
*/
@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菜单中选项的事件监听操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* menu菜单点击操作的监听事件
*/
@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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
/**
* 初始化 ActionBar内容
* */
private ActionBar actionBar;
private void initActionBar(){
actionBar= super .getActionBar();
actionBar.show();
//显示Home区域
actionBar.setDisplayShowHomeEnabled( true );
//设置home区域回退按钮
actionBar.setDisplayHomeAsUpEnabled( true );
actionBar.setHomeAsUpIndicator(R.drawable.back_move_details_normal);
//不显示Home区域标题
actionBar.setDisplayShowTitleEnabled( true ); //
actionBar.setTitle( "新闻" ); //设置title
//不显示Logo图片
actionBar.setDisplayUseLogoEnabled( false ); //
//去除home区域的Icon图标【将icon颜色设置为透明】
Drawable colorDrawable= new
ColorDrawable(android.R.color.transparent);
actionBar.setIcon(colorDrawable);
//自定义区域
actionBar.setDisplayShowCustomEnabled( true );
TextView tvTitle= new TextView( this ); //this,当前承载的
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);
}
|
实现效果:
以上所述就是本文的全部内容了,希望大家能够喜欢。