【问题标题】:Setting AutoCompleteTextView divider height has no effect in android设置 AutoCompleteTextView 分隔线高度在 android 中没有效果
【发布时间】:2017-10-13 20:16:46
【问题描述】:

这是我的 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" />

有谁知道为什么设置android:dividerHeight 没有效果?

【问题讨论】:

    标签: android android-layout autocompletetextview


    【解决方案1】:

    AutoCompleteTextView 是一个复合视图 - 它同时具有一个 EditText 组件和一个浮动 DropDown 组件。 EditText 组件的样式很简单,但DropDown 很难,因为它混合了AutoCompleteTextView 本身的属性和通过android:dropDownListViewStyle 在主题中设置的样式。

    如果你想改变分隔线,你必须创建一个主题并将其指向一个样式,这不是一个立竿见影的解决方案:

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

    但是请注意,这些样式更改将应用​​于您的整个应用程序。因此,如果您的 UI 中有其他 DropDown 组件,它们也可能会受到影响。

    【讨论】:

    • 感谢您的回答 (+1)。有没有办法不影响整个应用程序,无论多么复杂?
    • 你可以像这样将它应用到一个活动中:
    【解决方案2】:

    Autocompletetextview 下拉自定义项目分隔符可以使用下面的项目布局和可绘制来实现。 完整参考http://www.zoftino.com/android-autocompletetextview-custom-layout-and-adapter

    自定义布局

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

    自定义可绘制对象

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-03
      • 2021-05-04
      • 1970-01-01
      相关资源
      最近更新 更多