使用TextInputLayout.OutlinedBox样式的材质设计微调器

时间:2018-11-07 22:32:00

标签: android android-studio material-design

我当前正在使用Material Design TextInputLayout OutlinedBox,如下所示:

        <android.support.design.widget.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.design.widget.TextInputEditText
                android:id="@+id/myEditText"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Title"
                android:inputType="text"/>

        </android.support.design.widget.TextInputLayout>

我正在尝试在TextInputEditText下添加一个下拉框Spinner,并希望保持相同的样式:OutlinedBox。

我看到在Material Design Material Design Text Fields中似乎支持下拉菜单。如此处的区域所示:

As shown on here for the Area

我目前正在使用微调框来生成下拉列表。

        <Spinner
            style="@style/Widget.AppCompat.Spinner.DropDown"
            android:id="@+id/option"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:dropDownWidth="match_parent" />

似乎无法在OutlinedBox设计之后添加下拉列表。是否有一个可以让我实现这一目标的库,还是在Material Design中实现此目标的更好方法?

11 个答案:

答案 0 :(得分:7)

我假设您想在TextInputLayout内有一个Exposed下拉菜单,但我遇到了同样的问题,可以像在Windows的AutoCompleteTextView内那样使用TextInputLayout XML中的以下内容。这是我处理此问题的示例。

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:paddingRight="30dp"
            android:paddingEnd="30dp"
            tools:ignore="RtlSymmetry"
            android:layout_margin="5dp">

            <ImageView
                android:layout_width="30dp"
                android:layout_margin="10dp"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_location_city_black_24dp"
                android:layout_gravity="center"
                />




        <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="Type"
            android:orientation="horizontal"
            >



            <AutoCompleteTextView
                android:id="@+id/filled_exposed_dropdown"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="none"/>

        </com.google.android.material.textfield.TextInputLayout>


        </LinearLayout>

    </LinearLayout>

您还将需要项目布局资源来填充下拉弹出窗口。下面的示例提供了一种遵循“材料设计”准则的布局。

res / layout / dropdown_menu_popup_item.xml

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

根据需要添加以下代码。

String[] type = new String[] {"Bed-sitter", "Single", "1- Bedroom", "2- Bedroom","3- Bedroom"};

        ArrayAdapter<String> adapter =
                new ArrayAdapter<>(
                        this,
                        R.layout.dropdown_menu_popup_item,
                        type);

        AutoCompleteTextView editTextFilledExposedDropdown =
                findViewById(R.id.filled_exposed_dropdown);
        editTextFilledExposedDropdown.setAdapter(adapter);

以防万一这无法帮助您检查材料设计页面中的“暴露的下拉菜单”。 [https://material.io/develop/android/components/menu/][1]

这是我对堆栈溢出的第一个答案,希望对您有所帮助。

答案 1 :(得分:6)

答案 2 :(得分:3)

我认为该文档根本没有显示Spinner。我认为它显示的是带有下拉图标的TextInputLayout

Anatomy section的“图标”小节中,显示为

  

5。下拉图标

     

下拉箭头表示文本字段具有嵌套的选择组件。

现在,如何您不确定我提供的“嵌套选择组件” ...

答案 3 :(得分:2)

似乎他们实际上使用了TextInputLayout来包装AutoCompleteTextView。请注意,它们已经是实质性的Components主题[https://github.com/material-components/material-components-android/blob/master/docs/getting-started.md]

请参阅: https://material.io/design/components/menus.html#exposed-dropdown-menu https://material.io/develop/android/components/menu/

答案 4 :(得分:2)

只需使用样式为{strong> requireTextInputLayout中包含的Material Components Library

类似的东西:

Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu

enter image description here

答案 5 :(得分:2)

我使用以下方法解决了我的问题: 在XML中:

<com.google.android.material.textfield.TextInputLayout
        android:id="@+id/layout"
        style="@style/AppTheme.ExposedDropdownMenu"                        
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <AutoCompleteTextView
            android:id="@+id/my_spinner_dropdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    </com.google.android.material.textfield.TextInputLayout>

使用代码:

        layout.keyListener=null
        ArrayAdapter(it, android.R.layout.simple_list_item_1, list).also { 
         adapter ->
            layout.setAdapter(adapter)
        }

积分:How to make EditText not editable through XML in Android?

答案 6 :(得分:1)

从其他答案来看,“AutoCompleteTextView”是答案,但它的作用与微调器不同。

这是我的解决方案。只需将普通的 edittext 放在 TextInputLayout 中,并禁用此 editText 输入。并放置一个 0dp,0dp 微调器用于正常微调器工作。

不要让微调器可见性=消失,因为如果它消失了,微调器侦听器将不起作用

布局.xml

  <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/textInputLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/_10dp"
        android:theme="@style/ThemeOverlay.App.TextInputLayout">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:drawableEnd="@drawable/arrow_down_pacific_blue"
            android:focusable="false"
            android:hint="şehir"
            android:inputType="none" />

    </com.google.android.material.textfield.TextInputLayout>

    <Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="10dp"
        android:background="@android:color/transparent"
        android:spinnerMode="dialog"
        tools:listitem="@layout/general_spinner_item" />

java代码

将单击侦听器设置为编辑文本以触发微调器单击

findViewById(R.id.editText).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                    spinner.performClick();
                }
            });

在微调侦听器中,从所选项目设置编辑文本文本,

  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                selectedCity= (City) parent.getAdapter().getItem(position);

                editText.setText(selectedCity.getScreenText());

                RDALogger.debug("selectedObject " + selectedCity);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

和结果视图

enter image description here

答案 7 :(得分:1)

您可以查看我的Medium article where I introduce a custom MaterialSpinner,它支持双向数据绑定和选择跟踪。生成的布局看起来很简单:

<com.google.android.material.textfield.TextInputLayout
    android:id="@+id/input_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/selection_hint"
    app:startIconDrawable="@drawable/selection_icon"
    app:boxBackgroundMode="outline"
    app:endIconMode="@{viewModel.items == null || viewModel.items.size() != 0 ? TextInputLayout.END_ICON_DROPDOWN_MENU : TextInputLayout.END_ICON_NONE}">

    <com.example.MaterialSpinner
        android:id="@+id/items"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:items="@{viewModel.items}"
        app:selectedPosition="@={viewModel.selectedItemPosition}"
        app:emptyText="@string/selection_no_item_text" />

</com.google.android.material.textfield.TextInputLayout>

答案 8 :(得分:0)

我正在使用以下材料库来获得微调器

implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'

这是我的布局样子

 <com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
            android:layout_width="500dp"
            android:layout_height="wrap_content"
            android:hint="@string/select_wifi"
            app:hintTextAppearance="@style/hintStyle"
            app:startIconDrawable="@drawable/wifi">

            <com.google.android.material.textfield.MaterialAutoCompleteTextView
                style="@style/textInputEdittext"
                android:inputType="none" />

        </com.google.android.material.textfield.TextInputLayout>

查看此图片以了解微调器

enter image description here

答案 9 :(得分:0)

我知道现在回答这个问题为时已晚,但像我这样陷入此类或类似问题的人可能会发现这非常有用。根据要求访问此 repo 其完美解决方案。该库支持所有材质 TextInputLayout 样式。感谢 "Mamoon Al-hawamdeh" 提供这个惊人的库。

enter image description here

答案 10 :(得分:0)

所有这些答案都有帮助,但一个重要的注意事项是,如果您设置 app:endIconMode 属性,您的下拉菜单将不起作用。