我是使用ActionBar和SherlockActionBar的新手。
我要做的是在ActionBar(AB)中添加“菜单”,点击子菜单项后更改背景颜色,不要关闭菜单。
原因是因为我会尝试做某种类型的“过滤”
我添加了代码,其中应该更改背景颜色,但我不知道该怎么做。
单击项目时不要关闭菜单的选项我不知道它是否可能,因为我从未见过它。
public class MainActivity extends SherlockActivity{
ActionBar actionBar;
boolean boolSubitem1;
boolean boolSubitem2;
boolean boolSubitem3;
boolean boolSubitem4;
@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Sherlock___Theme_Light);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolSubitem1 = false;
boolSubitem2 = false;
boolSubitem3 = false;
boolSubitem4 = false;
actionBar = getSupportActionBar();
actionBar.setTitle("Testing");
actionBar.setSubtitle("Miau");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.item1:
return true;
case R.id.subitem1:
if(boolSubitem1) {
//CHANGE BACKGROUNDCOLOR
}
return true;
case R.id.subitem2:
if(boolSubitem2) {
//CHANGE BACKGROUNDCOLOR
}
return true;
case R.id.subitem3:
if(boolSubitem3) {
//CHANGE BACKGROUNDCOLOR
}
return true;
case R.id.subitem4:
if(boolSubitem4) {
//CHANGE BACKGROUNDCOLOR
}
return true;
}
return super.onOptionsItemSelected(item);
}
}
菜单的XML文件:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/item1"
android:orderInCategory="0"
android:title="Test1"
android:showAsAction="ifRoom">
<menu>
<item
android:id="@+id/subitem1"
android:title="SubMenu1"/>
<item
android:id="@+id/subitem2"
android:title="SubMenu2"/>
<item
android:id="@+id/subitem3"
android:title="SubMenu3"/>
<item
android:id="@+id/subitem4"
android:title="SubMenu4"/>
</menu>
</item>
</menu>
谢谢大家!
答案 0 :(得分:0)
我找到了一个解决我的问题的方法,有点像你的问题,因为我想改变溢出菜单文字的颜色和放大背景颜色。
我只是覆盖onCreateView(String name, Context context, AttributeSet args)
中的MainActivity.java
并验证创建的View
是否来自班级android.support.v7.view.menu.ListMenuItemView
。
然后,我只是在里面找到TextView
并更改其文本颜色(参见下面的代码)
代码:
@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Do you own inflater stuff here
// Check for menu items (Options menu AKA menu key)
if (name.equalsIgnoreCase("android.support.v7.view.menu.ListMenuItemView")) {
try {
// Ask our inflater to create the view
final View view = LayoutInflater.from(context).createView(name, null, attrs);
// Kind of apply our own background
new Handler().post(new Runnable() {
public void run() {
if (!Common.compatible(Common.color, 0xFF000000)) {
try {
((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFFFFFFFF);
} catch (ClassCastException e) {
}
} else {
try {
((TextView)((RelativeLayout)((ListMenuItemView)view).getChildAt(1)).getChildAt(0)).setTextColor(0xFF000000);
} catch (ClassCastException e) {
}
}
view.setBackgroundColor(Common.color);
}
});
return view;
} catch (InflateException e) {
} catch (ClassNotFoundException e) {
}
}
return null;
}