安卓简单下拉框条目

23
这个问题与这个问题有关:android.R.simple_spinner_item 由于我的声望不高,无法发表评论,我有一个额外的问题:
如果我复制并粘贴android.R.simple_spinner_item布局,则会在某些情况下出现错误。
android:layout_height="?android:attr/dropdownListPreferredItemHeight"

报错信息为"error: Error: Attribute is not public. (at 'layout_height' with value '?android:attr/dropdownListPreferredItemHeight')。"

我刚刚添加了android:gravity="right"以将下拉菜单项对齐到右侧。

请问如何解决这个错误?


2
请使用android:layout_height="wrap_content"。由于dropdownListPreferredItemHeight不是公共的,因此不能使用它。 - MysticMagicϡ
5个回答

42

如果你不加上android前缀,它对我来说似乎是有效的,就像这样:

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
             android:id="@android:id/text1"
             style="?android:attr/spinnerDropDownItemStyle"
             android:singleLine="true"
             android:layout_width="match_parent"
             android:layout_height="?attr/dropdownListPreferredItemHeight"
             android:ellipsize="marquee"/>

6
这并没有回答这个问题。 - arenaq
4
它为什么不回答问题?它直接、确切地回答了这个问题。 - Daniel Wilson
6
@DanielWilson 中的 android:layout_height="?attr/dropdownListPreferredItemHeight" 在 appcompat-v7 中被标记为私有的。 - EmmanuelMess

4
这个资源是私有的,只有包含该属性的库才能使用它。所以您需要获取此属性的大小并在您的应用程序中创建它。从源代码可以看出,您可以在https://android.googlesource.com/platform/frameworks/support/+/50fe5ec/appcompat/res/values/themes.xml的第50和/或84行找到该属性。因此,在您的dimens.xml文件中,您可以编写以下内容: <dimen name="dropdownListPreferredItemHeight">64dip</dimen> 然后像普通资源一样引用它: android:layout_height="@dimen/dropdownListPreferredItemHeight"

4

那么接下来你需要设计它。

布局/layout/my_spinner_textview.xml

 <?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/spinnerItemStyle"
    android:id="@android:id/text1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="10dp"
    android:gravity="right" />

这是我如何设置适配器的方式

 private String[] state= {"Andra Pradesh","Arunachal Pradesh","Assam","Bihar","Haryana","Himachal Pradesh", "Jammu and Kashmir", "Jharkhand","Karnataka", "Kerala","Tamil Nadu"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,  R.layout.my_spinner_textview, state);
        adapter_state.setDropDownViewResource(R.layout.my_spinner_textview);
        Spinner spinner=(Spinner)findViewById(R.id.spinner1);
        spinner.setAdapter(adapter_state);

    }

我知道,但这也会将对齐方式设置为左对齐。我想要默认视图并右对齐。 - Zuop
抱歉,我的意思是,这将包装内容,使得列表项比“dropdownListPreferredItemHeight”更小。如果我知道那个高度会是多少,我就可以设置高度...虽然不是一个很好的解决方案。 - Zuop
1
@Zuop 这是“64dip”。 - Aleks N.

2
android:layout_width="match_parent"
android:layout_height="48dp"

使用这些属性创建自定义布局。您可能会在之后进行主题设置。

1
你只能使用系统定义为公共的 Android 资源(主题或属性)。
由于属性 "?android:attr/dropdownListPreferredItemHeight" 不是公共资源,因此无法使用该属性。
相反,你可以使用 "android:layout_height="wrap_content"" 来设置 Spinner 的项布局。
另一种解决方法可能是将资源从 SDK 复制到项目中,然后在项目中使用它们。

2
我已经尝试过“自动换行”,但这会使项目的高度大幅缩小,因此太小了。我只想保持默认大小。 - Zuop
1
哇,每个人都给出了相同的可怕建议,这会删除每个项目之间的空格。为什么有人会满足于此呢? - Luke Allison

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接