【问题标题】:How to change Spinner's dropdown width?如何更改 Spinner 下拉菜单的宽度?
【发布时间】:2017-04-14 07:04:23
【问题描述】:


ma​​in.xml

<LinearLayout
    android:id="@+id/spinners"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:orientation="horizontal">
    <Spinner
        android:id="@+id/location_spinner"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:dropDownWidth="match_parent"/>
    <Spinner
        android:id="@+id/license_spinner"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:dropDownWidth="match_parent"/>
</LinearLayout>

spinner_row.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <Button
        android:id="@+id/btn_1"
        style="@style/SpinnerButton">
    </Button>

</LinearLayout>

style.xml

<style name="SpinnerButton" parent="@android:style/Widget.Button">
    <item name="android:layout_width">match_parent</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:layout_marginLeft">5dp</item>
    <item name="android:layout_marginRight">5dp</item>
    <item name="android:layout_marginBottom">10dp</item>
    <item name="android:textColor">@android:color/black</item>
    <item name="android:layout_weight">1</item>
    <item name="android:clickable">true</item>
</style>

我想要像 [图片 B] 这样的下拉样式。我用过dropDownWidth = match_parent。但它不起作用。我认为这是因为这个 dropDown 的父级是 Spinner。每个微调器的权重为 1。有没有办法应用宽下拉布局?

【问题讨论】:

标签: android xml spinner dropdown


【解决方案1】:

试试下面的代码,如果它适合你。

 DealCategorySpinner = (Spinner) findViewById(R.id.DealCategorySpinner);

    try {
        Field popup = Spinner.class.getDeclaredField("mPopup");
        popup.setAccessible(true);

        // Get private mPopup member variable and try cast to ListPopupWindow
        android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(DealCategorySpinner);

        // Set popupWindow width to 800px
        popupWindow.setWidth(800);
    }
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 2021-07-25
    • 1970-01-01
    • 1970-01-01
    • 2016-09-06
    相关资源
    最近更新 更多