【问题标题】:how to customize dialog size in spinner如何在微调器中自定义对话框大小
【发布时间】:2019-12-23 06:04:51
【问题描述】:

这是我的微调器代码

XML 代码

 <Spinner
            android:id="@+id/amountContainer"
            android:layout_width="0dp"
            android:layout_height="30dp"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:background="@drawable/spinner_border"
            android:spinnerMode="dialog"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/amountTitleTV" />

Kotlin 代码

val adapter = ArrayAdapter((this.activity as Context), android.R.layout.simple_spinner_item, entries.toList())
    adapter.setDropDownViewResource(R.layout.amount_picker)
    adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice)

如何为对话框设置宽度?比如页面宽度的 60% 甚至 300dp

【问题讨论】:

    标签: android android-layout kotlin android-spinner


    【解决方案1】:

    无法自定义对话框微调器,但您可以使用下拉模式自定义,它看起来像对话框微调器。

    将此属性添加到 xml 文件中的微调器,这将为您的微调器提供全宽

     // this width is for dialog
     android:dropDownWidth="300dp"
    
    //drop down with only work when spinner mode is drop down
    android:spinnerMode="dropdown"
    

    通过使用dropDownVerticalOffset 属性,您可以在微调器和它的下拉列表之间留出空间。这将有助于看起来像对话框微调器`

     android:dropDownVerticalOffset="@dimen/_2sdp"
    

    【讨论】:

    • 我只使用了这个 android:dropDownWidth="300dp" 因为我希望它是对话框而不是下拉菜单,但它不起作用,它只适用于下拉菜单而不是对话框
    • android:dropDownWidth 仅在 android:spinnerMode="dropdown" 时有效。我正在更新我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多