【问题标题】:Padding in arrayadapter with custom item on dropdown spinner使用下拉微调器上的自定义项在 arrayadapter 中填充
【发布时间】:2017-08-01 23:19:19
【问题描述】:

我想要一个带提示的下拉微调器,但我无法配置它。现在我设法通过覆盖我的适配器中的 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>

在我选择任何东西之前,它看起来像这样(没有填充):

当我选择某些东西时:

【问题讨论】:

    标签: android layout spinner


    【解决方案1】:

    你需要声明你的微调器高度

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

    【讨论】:

    • 我可能会在最后这样做。
    【解决方案2】:

    在您的微调器 item.xml 中:您正在使用 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
        />
    

    【讨论】:

    • 没有变化:(
    • 你是否也在主xml中添加了dp值
    • 尝试删除微调器标签中的style...查看Example
    • 我删除了样式并且没有看到任何更改。当我向承载微调器的相对布局添加填充时,类别仍然被减半。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多