【问题标题】:Android custom spinner with theme带有主题的 Android 自定义微调器
【发布时间】:2016-04-28 09:28:40
【问题描述】:

我正在尝试使用主题属性自定义我的微调器。 这是我的微调器项目布局

<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>

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

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

我做错了什么?

【问题讨论】:

    标签: android android-spinner android-theme


    【解决方案1】:

    你可以试试下面的代码

     // 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);
    

    【讨论】:

    • 当然我可以通过适配器自定义我的视图,但我想通过 xml 执行此操作。问题是为什么在 AndroidStudio 预览中一切正常?但在真实设备上它会崩溃。
    • Android 预览 只需显示 UI 即可。从 android preview 你不能通过异常来判断你的应用程序。它只是为了设计
    【解决方案2】:

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

    //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 ->
     }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-24
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      • 1970-01-01
      相关资源
      最近更新 更多