AutoCompleteTextView更改样式

本文详细介绍了如何定制Android中AutoCompleteTextView的样式,包括修改下拉列表的背景、分割线等属性,通过调整主题和样式实现高度自定义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


一、准备

AutoCompleteTextView,是EditText+PopupWindow(ListPopupWindow)结合的:


ListPopupWindow的构造方法之一为:

public ListPopupWindow(Context context) {
        this(context, null, com.android.internal.R.attr.listPopupWindowStyle, 0);

}

其中还包括

public DropDownListView(Context context, boolean hijackFocus) {
            super(context, null, com.android.internal.R.attr.dropDownListViewStyle);
            mHijackFocus = hijackFocus;
            // TODO: Add an API to control this
            setCacheColorHint(0); // Transparent, since the background drawable could be anything.
}

(在 buildDropDown()方法中创建对象DropDownListView


所以如果更改ListPopupWindow样式,覆盖listPopupWindowStyle主题,而更改DropDownListView样式,覆盖dropDownListViewStyle主题。


二,查看资源文件


platforms/android-xx/data/res/values/attrs.xml

<attr name="dropDownListViewStyle" format="reference" />

platforms/android-xx/data/res/values/themes.xml

<item name="listPopupWindowStyle">@android:style/Widget.ListPopupWindow</item>

<item name="dropDownListViewStyle">@android:style/Widget.ListView.DropDown</item>

(有相应的HOLO的样式,这里只是列出普通的)

platforms/android-xx/data/res/values/styles.xml


    <style name="Widget.ListView.DropDown">
        <item name="android:cacheColorHint">@null</item>
        <item name="android:divider">@android:drawable/divider_horizontal_bright_opaque</item>
    </style>


     <style name="Widget.Holo.Light.ListPopupWindow" parent="Widget.ListPopupWindow">
        <item name="android:dropDownSelector">@android:drawable/list_selector_holo_light</item>
        <item name="android:popupBackground">@android:drawable/menu_dropdown_panel_holo_light</item>
        <item name="android:dropDownVerticalOffset">0dip</item>
        <item name="android:dropDownHorizontalOffset">0dip</item>
        <item name="android:dropDownWidth">wrap_content</item>
     </style>


    <style name="Widget.ListPopupWindow">
        <item name="android:dropDownSelector">@android:drawable/list_selector_background</item>
        <item name="android:popupBackground">@android:drawable/spinner_dropdown_background</item>
        <item name="android:dropDownVerticalOffset">-10dip</item>
        <item name="android:dropDownHorizontalOffset">0dip</item>
        <item name="android:dropDownWidth">wrap_content</item>        
    </style>


三,实现

 <AutoCompleteTextView
                android:id="@+id/edit_search"
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:background="@drawable/search_et_bg"
                android:drawableLeft="@drawable/search_icon"
                android:drawablePadding="2dp"
               android:dropDownSelector="@color/transparent"
                android:dropDownVerticalOffset="3dp"

                android:hint="@string/search_hint"
                android:imeOptions="actionSearch"
                android:inputType="textFilter"
                android:paddingRight="25dp"
                android:singleLine="true"
                android:textColor="#000"
                android:textColorHint="#CCC"
                android:textSize="14sp" />


searchEt.setThreshold(0);
suggestAdapter = new SuggestAdapter(this, suggestWords, 10);
searchEt.setDropDownBackgroundResoter);
searchEt.setOnItemClickListener();urce(R.drawable.transparent_drawable);
searchEt.setAdapter(suggestAdaper)


主题:

<!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">

        ...

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

<style name="suggest_style" parent="@android:style/Widget.ListView.DropDown">
        <item name="android:background">@drawable/transparent_drawable</item>
        <item name="android:cacheColorHint">@android:color/transparent</item>
        <item name="android:divider">@null</item>
        <item name="android:listSelector">@android:color/transparent</item>
        <item name="android:scrollingCache">false</item>
        <item name="android:fadingEdge">none</item>
    </style>

其中:scrollingCache来源于:

 <declare-styleable name="AbsListView">
         <!-- Drawable used to indicate the currently selected item in the list. -->
        <attr name="listSelector" format="color|reference" />
        <!-- When set to true, the selector will be drawn over the selected item.
             Otherwise the selector is drawn behind the selected item. The default
             value is false. -->

        <attr name="drawSelectorOnTop" format="boolean" />
        <!-- Used by ListView and GridView to stack their content from the bottom. -->
        <attr name="stackFromBottom" format="boolean" />
        <!-- When set to true, the list uses a drawing cache during scrolling.
             This makes the rendering faster but uses more memory. The default
             value is true. -->

        <attr name="scrollingCache" format="boolean" />


.......


另外两篇文章

http://blog.stylingandroid.com/archives/1285

http://www.javacodegeeks.com/2013/06/android-listview-context-menu-actionmode-callback.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值