我想有一个带有提示的下拉微调器,但是我无法配置它。现在我设法通过覆盖我的适配器中的getItem来显示提示,但由于某种原因它忽略了填充。
category = (Spinner) view.findViewById(R.id.category);
List<String> categoryList = Arrays.asList(getResources().getStringArray(R.array.categories));
categoryAdapter = new ArrayAdapter<String>(getContext(), R.layout.spinner_item, categoryList){
@Override
public int getCount() {
if (!firsttime) {
firsttime = true;
return super.getCount(); // you dont display last item. It is used as hint.
}
return super.getCount()-1;
}
};
categoryAdapter.setDropDownViewResource(R.layout.spinner_item);
category.setAdapter(categoryAdapter);
category.setSelection(categoryAdapter.getCount());
xml中的微调器:
<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/category"
android:spinnerMode="dropdown"
android:paddingBottom="5sp"
android:paddingTop="5sp"
android:paddingStart="15sp"
android:paddingEnd="15sp"
android:backgroundTint="@color/text_selected" />
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:textSize="20sp"
android:textColor="@color/text_selected"
android:padding="5sp"
/>
字符串数组:
<string-array name="categories">
<item>For Sale</item>
<item>Services</item>
<item>Vehicles</item>
<item>Property</item>
<item>Category</item>
</string-array>
答案 0 :(得分:3)
您需要声明您的微调器高度
<Spinner
style="@style/Widget.AppCompat.DropDownItem.Spinner"
android:layout_width="match_parent"
android:layout_height="40dp" //like this
android:id="@+id/category"
android:spinnerMode="dropdown"
android:paddingBottom="5sp"
android:paddingTop="5sp"
android:paddingStart="15sp"
android:paddingEnd="15sp"
android:backgroundTint="@color/text_selected" />
答案 1 :(得分:1)
:你使用sp
代替填充dp
<?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:textSize="20sp"
android:textColor="@color/text_selected"
android:padding="6dp" //change here
/>