带有主题的Android自定义微调器

时间:2016-04-28 09:28:40

标签: android android-spinner android-theme

我试图用主题属性来剪切我的微调器。 这是我的微调器项目布局

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/DashboardSpinnerItemStyle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="test"
    android:textSize="@dimen/material_text_body1" />

我使用自定义样式

<style name="DashboardSpinnerItemStyle" parent="Widget.AppCompat.TextView.SpinnerItem">
    <item name="android:textColor">?dashboard_color</item>
    <item name="android:background">#00F</item>
</style>

我的主题确定了属性颜色

<style name="WhiteDashboardTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textColor">@android:color/white</item>
    <item name="dashboard_shape">@drawable/dashboard_center_shape_white</item>
    <item name="dashboard_color">@android:color/white</item>
    <item name="spinner_background">@drawable/dashboard_spinner_white</item>
    <item name="dashboard_spinner_item_style">@style/DashboardSpinnerItemStyle</item>
</style>

enter image description here

工作正常,但模拟器或真实设备会抛出异常

04-28 09:01:44.909 2460-2460/com.amocrm.prototype E/AndroidRuntime: FATAL EXCEPTION: main android.view.InflateException: Binary XML file line #2: Error inflating class TextView

我做错了什么?

2 个答案:

答案 0 :(得分:0)

  

您可以尝试以下代码

 // Initializing an ArrayAdapter
    final ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(
            this,R.layout.spinner_item,plantsList){
        @Override
        public View getDropDownView(int position, View convertView,
                                    ViewGroup parent) {
            View view = super.getDropDownView(position, convertView, parent);
            TextView tv = (TextView) view;

                // Set the Text color
                tv.setTextColor(Color.parseColor("#FFC9A3FF"));

            return view;
        }
    };
    spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
    spinner.setAdapter(spinnerArrayAdapter);

答案 1 :(得分:0)

尝试使用以下代码,它将帮助您在微调器下方显示项目。

//add below code in you style file
<style name="CustomDropDownTilStyle" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColorHint">#9797A2</item>
<item name="hintTextColor">@color/colorPrimary</item>
<item name="colorControlNormal">@color/colorAccent</item>
<item name="colorControlActivated">@color/colorAccent</item>
<item name="colorControlHighlight">@color/colorAccent</item>
//This is xml view
<com.google.android.material.textfield.TextInputLayout
style="@style/CustomDropDownTilStyle"
android:layout_margin="8dp"
android:hint="@string/hint">

<androidx.appcompat.widget.AppCompatAutoCompleteTextView
    android:id="@+id/spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusable="false"
    android:inputType="none"
    android:textColor="@color/colorPrimary" />
  </com.google.android.material.textfield.TextInputLayout>


 //This is your Kotlin code
 val adapter: ArrayAdapter<String> = ArrayAdapter<String>(context, android.R.layout.simple_spinner_dropdown_item, arrayListOf("A","B","C"))
spinner.apply {
setAdapter(adapter)
onItemClickListener = AdapterView.OnItemClickListener { parent, arg1, 
position, id ->
 }
}