我的 OptionMenu 中有一个按钮,当触摸它时会打开一个弹出菜单,其中包含在运行时获取并以编程方式添加的项目(因此不能使用硬编码的 xml 菜单项)。我想根据它们的值突出显示这些项目的一个子集,我使用了这里建议的内容:如何在 NavigationView 中自定义项目背景和项目文本颜色?尝试使项目具有不同的颜色。但是,尽管isChecked()价值不同,但所有项目的颜色都相同。
这是该问题的一个小工作示例:
MainActivity.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.actionbar_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch(id) {
case R.id.action_bar_button:{
showMenu();
return true;
}
}
return super.onOptionsItemSelected(item);
}
private void showMenu() {
View v = findViewById(R.id.action_bar_button);
Context wrapper = new ContextThemeWrapper(this, R.style.MyPopupMenu);
PopupMenu popupMenu = new PopupMenu(wrapper, v);
//Sample items to demonstrate the issue. I want the background to be red if false, blue if true
Map<String, Boolean> list = new HashMap<>();
list.put("Item 1", true);
list.put("Item 2", false);
list.put("Item 3", true);
for(Map.Entry<String, Boolean> entry : list.entrySet()){
String msg = entry.getKey();
MenuItem item = popupMenu.getMenu().add(msg).setCheckable(true).setChecked(entry.getValue());
System.out.println(item.getTitle() + ": " + item.isChecked());
}
popupMenu.show();
}
Run Code Online (Sandbox Code Playgroud)
style.xml包含:
<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:itemBackground">@drawable/menu_item_background</item>
</style>
Run Code Online (Sandbox Code Playgroud)
actionbar_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_bar_button"
android:title="List"
android:icon="@drawable/ic_launcher_foreground"
app:showAsAction="always" />
</menu>
Run Code Online (Sandbox Code Playgroud)
color.xml包含:
<color name="red">#ff0000</color>
<color name="blue">#0000FF</color>
和menu_item_background.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/blue" android:state_checked="true"/>
<item android:drawable="@color/red" android:state_checked="false"/>
</selector>
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我得到以下信息:

如您所见,尽管isCheckedItem 1 和 3的状态为真,但它们仍然显示为红色。logcat 输出证实了这一点。
作为一个实验,我改变了menu_item_background.xml使用android:state_enabled而不是检查,它按预期工作:
这里发生了什么?为什么这不起作用android:state_checked?
谢谢你的帮助。
如果您的主要动机是突出显示所选项目或类似问题。我想建议作为对我有用的使用微调器的解决方案之一,其中弹出菜单提供与微调器类似的下拉选项,具有自定义下拉菜单布局,并更改微调器getDropDownView()适配器处的背景颜色。
R.layout.spinner_item.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_context"
android:layout_height="wrap_content"
android:visibility="gone"
/>
Run Code Online (Sandbox Code Playgroud)
R.layout.spinner_drop_down.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
>
<TextView
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="wrap_content"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:id="@+id/spinner_dropdown_text"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
用户界面设计
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!--Some Layouts-->
<Spinner
android:id="@+id/spinner"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center_vertical"
android:background="@drawable/icon"
android:gravity="center"
android:textDirection="firstStrongLtr"
android:overlapAnchor="false"/>
<!--Some More Layouts-->
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
val spinner = findViewById<Spinner>(R.id.spinner)
val spinnerData = arrayListOf<String>("Item 1", "Item 2", "Item 3")
val arrayAdapter = object : ArrayAdapter<String>(activity!!.applicationContext,R.layout.spinner_item, spinnerData) {
override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
val spinnerItem = layoutInflater.inflate(R.layout.spinner_drop_down, null)
val dropDownText = spinnerItem.findViewById<TextView>(R.id.spinner_dropdown_text)
dropDownText.text = spinnerData[position]
val selected = spinner.selectedItemPosition
if (position == selected) spinnerItem.setBackgroundColor(Color.GRAY)
return spinnerItem
}
}
arrayAdapter.setDropDownViewResource(R.layout.spinner_drop_down)
spinner.adapter = arrayAdapter
spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
//Do whatever you want to do when Item selected
}
override fun onNothingSelected(parent: AdapterView<*>) {
//Do whatever you want to do when No Item selected
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1179 次 |
| 最近记录: |