【问题标题】:Creating a material spinner dropdown in android using MDC使用 MDC 在 android 中创建材质微调器下拉列表
【发布时间】:2019-03-31 23:56:53
【问题描述】:

我正在研究 Material design Material.io 的材料组件,一切都很好,我正在尝试使用 MDC 的 TextField 组件 创建材料下拉微调器,但我似乎找不到任何相关文档,是否可以使用 MDC 创建微调器?如果是这样,我在哪里可以找到它的文档?

在 TextField 的目录中看到了一个微调器,我可以这样做吗?:

【问题讨论】:

  • 尝试将此主题应用到您的微调器style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox"
  • 是的,试过了,好像不行,甚至用材质布局包起来。
  • 好的,那么另一种方法就像补丁一样,但会起作用,使用TextInputLayout和上面的样式,在它里面的EditText把右边的drawable作为下拉图标,让所有的东西都可以编辑false并打开单击它时弹出菜单。它会完全按照您的要求运行
  • hacky,但可行。暂时不用,谢谢。
  • 当然,会深入挖掘并找到一些东西,如果有什么东西请告诉我。如果遇到任何问题,也请告诉我。

标签: android material-design mdc-components


【解决方案1】:

在 Material Design 网站上,它标记为 Planned for Android (Material Menus) 我还注意到 Material Design 的 Twitter feed 刚刚发布了用于 Web。所以希望很快就会发布一个实际的实现。

【讨论】:

    【解决方案2】:

    这正是你所需要的

    https://material.io/develop/android/components/menu/#exposed-dropdown-menus

    首先在 Textinputlayout 中添加 AutocompleteTextView

    <com.google.android.material.textfield.TextInputLayout
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/hint_text">
    
      <AutoCompleteTextView
          android:id="@+id/filled_exposed_dropdown"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"/>
    
    </com.google.android.material.textfield.TextInputLayout>
    

    然后你可以像这样设计菜单项:

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="16dp"
        android:ellipsize="end"
        android:maxLines="1"
        android:textAppearance="?attr/textAppearanceSubtitle1"/>
    

    在java中初始化适配器,如:

    String[] COUNTRIES = new String[] {"Item 1", "Item 2", "Item 3", "Item 4"};
    
    ArrayAdapter<String> adapter =
            new ArrayAdapter<>(
                getContext(),
                R.layout.dropdown_menu_popup_item,
                COUNTRIES);
    
    AutoCompleteTextView editTextFilledExposedDropdown =
        view.findViewById(R.id.filled_exposed_dropdown);
    editTextFilledExposedDropdown.setAdapter(adapter);
    

    您可以更改样式以满足各种变化,例如:

    填充 style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu"

    概述

    将此样式应用于您的 TextInputLayout:

    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"

    密集填充

    将此样式应用于您的 TextInputLayout:

    style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense.ExposedDropdownMenu"

    密集轮廓

    将此样式应用于您的 TextInputLayout:

    style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense.ExposedDropdownMenu"

    【讨论】:

    • 你能提到Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu的依赖
    • 将此添加到您的 gradle 实现“com.google.android.material:material:1.1.0-alpha09”
    • 如何避免键盘输入到这个 EditText ?
    • @petyr 在 Kotlin 中:binding.dropdownMenu.apply { this.setText(LIST_COUNTRIES.get(0), false) this.setAdapter(adapter) } 所以基本上是通过调用 setText("Default Value", false) 函数。
    • @Akashkumar 您可以使用各种不同的方法,可以使用数据绑定并将文本绑定到变量,或者使用 onItemSelectedListener
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多