Setting AutoCompleteTextView divider height has no effect in android

时光总嘲笑我的痴心妄想 提交于 2019-12-24 11:34:20

问题


Here is my AutoCompleteTextView

<AutoCompleteTextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="4dp"
        android:gravity="center"
        android:inputType="textCapWords|textAutoCorrect"
        android:textColor="@color/font_autocomplete"
        android:textSize="18sp" />

Does anyone know why setting the android:dividerHeight has no effect?


回答1:


An AutoCompleteTextView is a compound View - it's got both an EditText component and a floating DropDown component. The EditText component is straightforward to style, but the DropDown is difficult because it's a mixture of attributes on the AutoCompleteTextView itself and styles set in the theme via android:dropDownListViewStyle.

If you want to change the dividers, you have to create a theme and point that to a style, which isn't an immediately obvious solution:

<style name="MyTheme">
  <item name="android:dropDownListViewStyle">@style/DropDownListViewStyle</item>
</style>

<style name="DropDownListViewStyle">
  <item name="android:dividerHeight">4dp</item>
</style>

Note, however, that these style changes will apply across your whole Application. So if you've got other DropDown components in your UI, they'll likely be affected as well.




回答2:


Autocompletetextview drop down custom item divider can be implemented using below item layout and drawable. For complete reference http://www.zoftino.com/android-autocompletetextview-custom-layout-and-adapter

Custom layout

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/textView"
        style="?android:attr/dropDownItemStyle"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:textAppearance="?android:attr/textAppearanceLargePopupMenu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="6dp"
        android:enabled="false"
        android:background="@drawable/divider"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

</android.support.constraint.ConstraintLayout> 

Custom drawable

 <?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:tint="#42a5f5"
    android:shape="rectangle">
    <corners
        android:radius="4dp"/>
    <size
        android:height="6dp" />
    <solid android:color="#42a5f5" />
</shape> 


来源:https://stackoverflow.com/questions/24274590/setting-autocompletetextview-divider-height-has-no-effect-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!