java - 如何使用 Android Selector 更改 PopupMenu 中的项目颜色?
问题描述
我的 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();
}
样式.xml包含:
<style name="MyPopupMenu" parent="Widget.AppCompat.PopupMenu">
<item name="android:itemBackground">@drawable/menu_item_background</item>
</style>
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>
颜色.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>
但是,当我运行它时,我得到以下信息:
正如您所看到的,尽管第 1 项和第 3 项的状态为真,但它们仍然显示为红色。logcat 输出证实了这一点。 isChecked
作为一项实验,我将其更改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"
/>
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>
用户界面设计
<?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>
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
}
}
推荐阅读
- c# - ASP.NET Core 3.1 - HTTP 错误 500.30 - ANCM 进程内启动失败
- microsoft-graph-api - Microsoft Graph 人员 API - 主题搜索
- c++ - 如何重置 QCustom3DLabel 上的文本?
- python - sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:项目
- javascript - 如何使用 PHP、Java Script 构建 Web 小部件并向外部站点提供自定义 CSS 控件?
- jquery - 什么是 jquery 中的 .kendobutton() 方法以及如何取消绑定和绑定此按钮的“单击”事件?
- html - webView中的HTML视频
- javascript - 如何从第二个 Javascript 文件中获取标签元素(标题、src 等)
- php - 防止 Laravel 中原始条件的 Sql 注入
- android - Android logcat没有显示异常完整数据?