我正在寻找一些解决方案并尝试完成它。分频器仍然没有显示出来。
我在styles.xml中为我的主题设置了样式:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Others theme here. -->
<item name="android:dropDownListViewStyle">@style/mySpinnerStyle</item>
</style>
这是mySpinnerStyle
<style name="mySpinnerStyle" parent="android:Widget.ListView.DropDown">
<item name="android:divider">#00ff00</item>
<item name="android:dividerHeight">1dp</item>
</style>
这是我的微调器的片段代码:
Spinner chooseDate = (Spinner) view.findViewById(R.id.chooseDate);
String[] date_group = dateList.toArray(new String[dateList.size()]);
ArrayAdapter<String> itemDate = new ArrayAdapter<String>(getActivity(), R.layout.spinner, date_group);
itemDate.setDropDownViewResource(R.layout.spinner);
chooseDate.setAdapter(itemDate);
这是我的微调器布局xml:
<Spinner
android:theme="@style/mySpinnerStyle"
android:spinnerMode="dialog"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:id="@+id/chooseDate"
android:layout_width="match_parent"
android:layout_height="38dp"
android:background="@drawable/spinner_arrow" />
然后R.layout.spinner xml是这样的:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:textSize="22dp"
android:theme="@style/mySpinnerStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:paddingRight="40dp"
android:singleLine="true" />
我为我的微调器设置了样式,但我无法显示分隔符。我不知道为什么?
任何帮助将不胜感激。谢谢你。
清单使用我的AppTheme:
<application
android:allowBackup="true"
android:icon="@drawable/app_icon_512x512"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="android.support.multidex.MultiDexApplication">
答案 0 :(得分:5)
将分隔符显示为微调器项需要将两个属性设置为线性布局 1. 分频器和显示分频器检查下面的代码。
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="?android:attr/listDivider"
android:showDividers="end"
android:orientation="vertical">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/dp5"
android:textColor="#6F6F6F"
android:textAllCaps="false"
android:layout_marginStart="@dimen/dp5"
android:layout_marginLeft="@dimen/dp5" />
</LinearLayout>
答案 1 :(得分:3)
从android:theme="@style/mySpinnerStyle"
(R.layout.spinner xml)中删除TextView
并从spinner
android:spinnerMode="dialog"
使用此值
android:spinnerMode="dropdown"
答案 2 :(得分:2)
这可能会对你有帮助,
<强> activity_main.xml中强>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
tools:context=".MainActivity"
android:background="#effbe4"
>
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
<强> spinner_item.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="@drawable/spinner_item_border"
/>
<强> spinner_item_border.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#ff2632"/>
</shape>
</item>
<item android:bottom="2dp">
<shape android:shape="rectangle">
<solid android:color="#d7ffa2"/>
</shape>
</item>
</layer-list>
活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get reference of widgets from XML layout
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Initializing a String Array
String[] plants = new String[]{
"Black birch",
"Bolean birch",
"Canoe birch",
"Cherry birch",
"European weeping birch"
};
// Initializing an ArrayAdapter
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
this,R.layout.spinner_item,plants
);
spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
spinner.setAdapter(spinnerArrayAdapter);
}
}