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

问题描述 投票:13回答:4

我已经在网站上看到过这个问题,但是没有人能得到任何答案。

在Android中使用AutocompleteTextview时,有没有办法在下拉列表中自定义分隔符的外观?

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

(不是textview,我已经知道了)

android drop-down-menu autocompletetextview separator divider
4个回答
18
投票

我不知道如何为单个AutoCompleteTextView做到这一点,但我知道如何为整个应用程序设置它。这也应该对你有所帮助:)

<style name="MyTheme" parent="AppTheme">
        <item name="android:dropDownListViewStyle">@style/MyListViewStyle</item>
</style>

<style name="MyListViewStyle" parent="@android:style/Widget.ListView">
        <item name="android:divider">#F00</item>
        <item name="android:dividerHeight">1px</item>
</style>

1
投票

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

<?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>

因此,我正在做的是我有线性布局的背景颜色和文本视图的另一种背景颜色(textView的背景颜色与线性布局的背景颜色重叠)现在使用margin属性并将textview的下边距设置为1dp u'll在元素之间划清界限......


1
投票

我遇到了同样的问题并尝试了许多方法但没有达到结果,最后我得到了一个快速简便的方法来设置分隔符和分频器长度与AutocompleteTextview。基本上我们可以使用ArrayAdapter Layout的TextView从底部设置边框。像

步骤1.为arrayadapter创建一个布局,如as

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

step2:在drawable文件夹中创建新的布局,其名称为background_single_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>

最后看起来像分频器。

enter image description here


0
投票

Mark的答案通常是最好的解决方案。但是,不同版本的AppCompat库有不同的行为,关于android:dropDownListViewStyle如何影响其他菜单,如系统溢出/选项菜单。例如,在AppCompat版本23.0.1中,它不会影响ActionBar / Toolbar中的此菜单;而版本23.2.1将影响它,正如here所解释的那样。

如果您希望隔离的样式仅影响AutoCompleteTextView,则可能无法实现。但另一种方法是手动创建列表分隔符图像,作为下拉列表行布局的一部分:

RES /布局/ autocomplete_list_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>

RES /抽拉/ divider.xml

<?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);

使用ConstraintLayout here的类似方法。

© www.soinside.com 2019 - 2024. All rights reserved.