通过xml从AutoCompleteTextView中删除下划线

问题描述 投票:0回答:6

我有这样的观点:

<AutoCompleteTextView
        android:id="@+id/search_bar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/rect_search"
        android:layout_alignLeft="@+id/rect_search"
        android:layout_alignRight="@+id/rect_search"
        android:layout_alignTop="@+id/rect_search"
        android:hint="@string/search_bar"
        android:textSize="20sp" />

如您所见,该视图进入另一个视图 rect_search,即 ImageView。 现在我注意到 AutoCompleteTextView 小部件底部有一个默认下划线,我将删除它。有可能吗?

android textview autocompletetextview
6个回答
32
投票

只需输入:

android:background="@android:color/transparent"

在你的视野中。

编辑:正如@kiya所说,现在更好用

android:backgroundTint="@android:color/transparent"

因为它保持视图尺寸。


3
投票

android:inputType="textPhonetic"


1
投票

这也能正常工作

android:inputType="textNoSuggestions"

0
投票

如果您想删除下划线并保持大小,请使用以下代码:

android:backgroundTint="@android:color/transparent"

0
投票

我做了很多尝试来构建一个具有正确颜色的下拉菜单,并聚焦具有圆角的材质外观选项。

我会分享它来帮助其他人,结果会是这样的: 非对焦模式下:

在聚焦模式下并激活下拉菜单:

目前唯一的问题是,如果有人可以通过某种方式集中下拉菜单(例如使用键盘上的 TAB 按钮),他/她可以在文本框中输入内容,但在触摸模式下,就不会有问题 这是布局中应使用的 XML:

<com.google.android.material.textfield.TextInputLayout
        style="@style/CustomDropDownLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="50dp"
        android:layout_marginEnd="50dp"
        android:padding="5dp"
        app:boxCornerRadiusBottomEnd="18dp"
        app:boxCornerRadiusTopEnd="18dp"
        app:boxCornerRadiusBottomStart="18dp"
        app:boxCornerRadiusTopStart="18dp"
        app:boxStrokeWidth="0dp"
        >


    <AutoCompleteTextView
            android:background="@drawable/drop_down_background"
            android:id="@+id/userNameDropdown"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="none" />

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

将自定义样式放入:

res\values\customstyles.xml
文件:

<?xml version="1.0" encoding="utf-8"?>
<resources>



    <style name="CustomDropDownLayout" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
        <item name="helperTextTextAppearance">@style/TextInputLayoutHelperStyle</item>
        <item name="errorTextAppearance">@style/TextInputLayoutErrorStyle</item>
        <item name="hintTextAppearance">@style/TextInputLayoutHintStyle</item>
        <item name="boxStrokeColor">@color/text_input_layout_dropdown_hint_color</item>
    </style>



    <style name="TextInputLayoutErrorStyle" parent="TextAppearance.Design.Error">
        <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item>
    </style>

    <style name="TextInputLayoutHintStyle" parent="TextAppearance.Design.Hint">
        <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item>

    </style>

    <style name="TextInputLayoutHelperStyle" parent="TextAppearance.Design.HelperText">
        <item name="fontPath">fonts/Yekan-Bakh-Fat.ttf</item>
    </style>
    <style name="TextLabel" parent="TextAppearance.Design.Hint">
        <item name="android:textSize">20sp</item>
        <item name="android:textColor">@color/input_layout_not_focused</item>
    </style>



</resources>

并将此颜色放入单个文件中,例如

res\color\text_input_layout_dropdown_box_color.xml
:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_focused="true" />
    <item android:color="@color/colorPrimary" android:state_hovered="true" />
    <item android:color="@color/colorPrimary" android:state_enabled="false" />
    <item android:color="@color/input_layout_not_focused" />  <!-- unfocused -->
</selector>

还有另一个名为

\res\color\text_input_layout_dropdown_hint_color.xml

的文件
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_focused="true" />
    <item android:color="@color/colorPrimary" android:state_hovered="true" />
    <item android:color="@color/colorPrimary" android:state_enabled="false" />
    <item android:color="@color/input_layout_not_focused" />  <!-- unfocused -->
</selector>

并将文件放入可绘制对象中

res\drawable\drop_down_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">


    <solid android:color="#FFFFFF" />
    <corners
            android:radius="20dp" />
    <stroke
            android:width="2dp"
            android:color="@color/text_input_layout_dropdown_box_color" />

</shape>

并使用这样的自定义适配器:

ArrayAdapter dropDownAdpater = new ArrayAdapter<>(this,
        R.layout.dropdown_menu_popup_item, users);
userNameDropDownMenuAutoCompleteTextView.setAdapter(dropDownAdpater);
//set first item as selected one
if (users != null && users.size() > 0) {
    userNameDropDownMenuAutoCompleteTextView.setText(dropDownAdpater.getItem(0).toString(), false);
}
userNameDropDownMenuAutoCompleteTextView.setDropDownBackgroundResource(R.drawable.drop_down_background);
userNameDropDownMenuAutoCompleteTextView.setOnTouchListener((view, motionEvent) -> {
    userNameDropDownMenuAutoCompleteTextView.showDropDown();
    return true;
});

最后在文件中的布局中添加一个文件

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

-1
投票

如果要删除下划线并保持大小,请使用以下代码:app:boxStrokeWidth="0dp"

=>

    android:id="@+id/textInputLayout2"

    android:layout_width="345dp"
    android:layout_height="65dp"
    android:background="color/transparent"
    android:backgroundTint="@color/transparent"
    app:endIconMode="dropdown_menu"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:boxBackgroundColor="@color/transparent"
      android:scrollIndicators="none"
     app:boxStrokeWidth="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:startIconDrawable="@drawable/people"
    app:startIconTint="@color/btnbackground">

    <AutoCompleteTextView

        android:id="@+id/autotext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/button_corner_border"

        android:inputType="text"
        android:textColor="@color/darkbringle" />

</com.google.android.material.textfield.TextInputLayout>
© www.soinside.com 2019 - 2024. All rights reserved.