【问题标题】:android spinner dropdown customandroid微调器下拉自定义
【发布时间】:2016-07-12 17:41:51
【问题描述】:

我用 spinner 来展示一些东西,这是我的代码:

ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(), android.R.layout.simple_spinner_dropdown_item, nameList);
                adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

使用此代码,当单击微调器时,我可以看到文本和复选框。
之后,我尝试像这样自定义微调器:

ArrayAdapter<ClassName> adapter = new ArrayAdapter<ClassName>(getActivity(),
                        R.layout.list_spinner, tenfavList);
                adapter.setDropDownViewResource(R.layout.list_spinner_dropdown);

这是 list_spinner_dropdown.xml :

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:singleLine="true"
android:ellipsize="end"
android:textColor="#0004ff"
android:textSize="@dimen/20sp"/>

当使用此代码时,当单击微调器时,我只看到文本,没有复选框。
如何解决?

【问题讨论】:

  • 请正确写出你想要做什么的问题。这里没有什么是明确的,你想设置复选框或微调器。你使用选中的文本视图而不是微调器为什么?
  • 我的意思是,当单击微调器时,将显示下拉菜单,并且带有文本,带有此复选框。但在这里,我看不到复选框
  • 所以使用微调器并在包含复选框的适配器中添加自定义布局。如果你愿意,我会给你一个示例链接
  • 是的,我的意思是这样,使用 adapter.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);对不起我的英语不好。
  • 我正在重写我的问题,对不起。

标签: android spinner


【解决方案1】:

好的,现在我得到了你的问题。问题只是您请在 CheckedTextView 中添加样式以进行选中或未选中,例如:

在 res/drawable 中添加文件 default_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>

<item android:state_checked="true"
    android:drawable="@drawable/checkbox_checked" /> <!-- checked -->

<item android:state_pressed="true"
    android:drawable="@drawable/checkbox_checked" /> <!-- pressed -->

<item android:drawable="@drawable/checkbox_default" /> <!-- default -->

在您的 XML 中:

 <CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAllCaps="true"
        android:singleLine="true"
        android:ellipsize="end"
        android:checked="true"
        android:checkMark="@drawable/default_checkbox"
        android:text="sdfsdfsdfsdfdsfdsf"
        android:textColor="#0004ff"
        android:textSize="25dp"/>

【讨论】:

  • 请检查编辑的答案,让我知道它是否有用
  • 在 default_checkbox.xml 中,我看到“必须声明元素项”,这是错误。为什么?
  • 添加selector作为根标签
  • 您是否在drawable文件夹中添加了default_checkbox并根据选中或未选中更改您的复选框图像
  • 很高兴,很高兴为您提供帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
  • 2021-05-13
  • 2011-12-26
相关资源
最近更新 更多