我使用微调器显示下拉列表。我希望列表中的项目具有圆角。因此,我使用9色块包含一个带有圆角的图像(在角的外部透明)作为视图项背景,并使用选择器在按下时显示不同颜色的9色块。
问题:当我按下微调器列表中的项目时,我可以在角落看到蓝色背景,其中9色块是透明的。
我似乎无法摆脱按下项目时出现的蓝色背景。如果删除微调器中的9个补丁和任何设置,则可以看到列表中的项目视图默认为灰色,按下时为蓝色。
我还尝试不使用9个色块作为背景,而只是使用颜色选择器,并将选择器中按下的颜色设置为透明。然后,当我按下该项目时,它不是透明的而是蓝色的。我认为列表中的视图确实是透明的,但是当按下时背景中仍然存在蓝色...
我使用自定义SpinnerAdapter创建项目视图。这是简化的代码:
private class MySpinnerAdapter implements SpinnerAdapter {
@Override
public View getDropDownView(int i, View recycledView, ViewGroup viewGroup) {
View view = new View(context);
view.setBackground(context.getResources().getDrawable(R.drawable.testspinner));
view.setMinimumHeight(100);
return (View) view;
}
}
Run Code Online (Sandbox Code Playgroud)
用于背景的选择器。这里只有一种颜色,没有9色块。按下的颜色应该是透明的:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:drawable="@android:color/transparent" />
<item
android:drawable="@android:color/holo_purple" />
</selector>
Run Code Online (Sandbox Code Playgroud)
我在微调器上设置了自定义适配器:
spinner.setAdapter(new MySpinnerAdapter());
Run Code Online (Sandbox Code Playgroud)
而微调器是从XML布局中获取的:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="wrap_content">
<Spinner
android:id="@+id/myDropDown"
android:spinnerMode="dropdown"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dropDownWidth="match_parent"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我曾尝试在微调器上设置许多不同的属性,并尝试了一些样式属性,但是我无法摆脱这种蓝色背景...
感谢@Vikram 指出样式的使用。
但是,经过调查发现 Spinner 中按下的项目的背景不是来自您建议的属性,而是来自android:listSelector. 所以我可以解决这样的问题:
在styles.xml 中定义一个新样式:
<style name="MyListView">
<item name="android:listSelector">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
在 themes.xml 中定义一个新主题:
<style name="MyTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
<item name="android:dropDownListViewStyle">@style/MyListView</item>
</style>
Run Code Online (Sandbox Code Playgroud)
将主题应用于我在 AndroidManifest.xml 中的活动:
<activity android:name=".MyActivity"
...
android:theme="@style/MyTheme">
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2704 次 |
| 最近记录: |