Android 在AutocompleteTextview下拉列表中自定义分隔符/分隔符

Android 在AutocompleteTextview下拉列表中自定义分隔符/分隔符,android,drop-down-menu,autocompletetextview,separator,divider,Android,Drop Down Menu,Autocompletetextview,Separator,Divider,我在网站上看到过其他人问这个问题,但没有人能得到任何答案 在android中使用AutocompleteTextview时,有没有办法自定义下拉菜单中分隔符的外观 对于ListView来说很容易,但是对于autocompletetextview,只使用ArrayAdapter,有没有办法自定义分隔符呢 (不是文本视图,我已经知道了)我不知道如何为单个AutoCompleteTextView进行设置,但我知道如何为整个应用程序进行设置。这也应该对您有所帮助:) @样式/MyListViewSty

我在网站上看到过其他人问这个问题,但没有人能得到任何答案

在android中使用AutocompleteTextview时,有没有办法自定义下拉菜单中分隔符的外观

对于ListView来说很容易,但是对于autocompletetextview,只使用ArrayAdapter,有没有办法自定义分隔符呢


(不是文本视图,我已经知道了)

我不知道如何为单个AutoCompleteTextView进行设置,但我知道如何为整个应用程序进行设置。这也应该对您有所帮助:)


@样式/MyListViewStyle
#F00
1px

我没有找到任何分隔符属性,所以我这样做了

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/black">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/textView129"
    android:textColor="#000000"
    android:background="@color/white"
    android:layout_marginBottom="1dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp"/>
</LinearLayout>


所以我要做的是我有一个线性布局的背景色和另一个文本视图的背景色(文本视图的背景色与线性布局的背景色重叠)现在,使用margin属性并将textview的底部边距设置为1dp,您将在元素之间获得一条清晰的线……

我也遇到了同样的问题,尝试了许多方法,但都没有达到效果,最后我找到了一种使用AutocompleteTextview快速简便地设置分隔符和分隔符长度的方法。基本上,我们可以使用ArrayAdapter布局的TextView从底部设置边框。像

第一步。如图所示,为ArrayAdapter创建布局

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/testt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/background_single_line"
android:textColor="@drawable/text_color"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
style="?android:attr/dropDownItemStyle"
android:maxLines="1"
android:padding="3dp"
android:textSize="16sp" />

步骤2:在drawable文件夹中创建新布局,其名称为background\u single\u line.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <solid android:color="@android:color/black" />
    </shape>
</item>
<item android:bottom="1dp">
    <shape android:shape="rectangle">
        <solid android:color="@android:color/white" />
    </shape>
</item>

最后看起来像是分隔符

通常,来自的答案是最好的解决方案。然而,不同版本的AppCompat库有着不同的行为,关于
android:dropDownListViewStyle
如何影响其他菜单,如系统溢出/选项菜单。例如,在AppCompat版本23.0.1中,它不会影响ActionBar/工具栏中的此菜单;如前所述,23.2.1版将对其产生影响

如果您想要一个只影响AutoCompleteTextView的独立样式,则可能无法实现。但另一种方法是手动创建列表分割器图像,作为下拉列表行布局的一部分:

res/layout/autocomplete\u list\u row.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:layout_height="50dip">

    <TextView
        android:id="@+id/text"
        style="?android:attr/dropDownItemStyle"
        android:textSize="18sp"
        android:layout_width="fill_parent"
        tools:text="An auto-complete list item"
        android:minHeight="48dip"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="@drawable/divider"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="#458c8c8c" />
</shape>

res/drawable/divider.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:layout_height="50dip">

    <TextView
        android:id="@+id/text"
        style="?android:attr/dropDownItemStyle"
        android:textSize="18sp"
        android:layout_width="fill_parent"
        tools:text="An auto-complete list item"
        android:minHeight="48dip"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"/>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dip"
        android:background="@drawable/divider"/>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
       android:shape="rectangle">
    <size android:height="1dp" />
    <solid android:color="#458c8c8c" />
</shape>

然后在适配器中为AutoCompleteTextView使用此布局:

ArrayAdapter<CharSequence> autoCompleteListAdapter = 
    new ArrayAdapter<>(context,
                       R.layout.autocomplete_list_row,
                       R.id.text, arrayList);
ArrayAdapter autoCompleteListAdapter=
新ArrayAdapter(上下文,
R.layout.autocomplete\u list\u行,
R.id.text,arrayList);

使用ConstraintLayout的方法类似。

我将1px替换为1dp。如果要为单个AutoCompleteTextView或任何其他视图设置分隔符,只需为View->android:theme=“@style/MyTheme”设置分隔符。它将仅适用于此视图。您还可以将
android:divider
设置为可绘制