设置Android AutoComplete的样式

设置Android AutoComplete的样式,第1张

概述我有一个要应用某些样式的AutoCompleteTextView.代码可以正常工作,基本样式也可以,但是我想更改一些更高级的内容.特别:自定义字体我的搜索栏使用我的应用程序的自定义字体,但是预测行使用系统的默认字体显示.这是我为搜索结果设置字体的方式:mSearchTextView.setTypeface(font.m

我有一个要应用某些样式的autoCompleteTextVIEw.代码可以正常工作,基本样式也可以,但是我想更改一些更高级的内容.特别:

自定义字体

我的搜索栏使用我的应用程序的自定义字体,但是预测行使用系统的默认字体显示.这是我为搜索结果设置字体的方式:

mSearchTextVIEw.setTypeface(Font.mAvenirLTStandardlight);

从下拉菜单中删除阴影

默认的下拉选项带有阴影,我的应用程序使用更扁平的设计.如果可能的话,我想删除它.

在下拉菜单中添加半径

我能够对每个结果线的半径进行取整,但是无法弄清楚如何将曲线应用于整个下拉框.

这是我适用的代码部分:

private voID setautoCompleteListener() {    autoCompleteAdapter adapter = new autoCompleteAdapter(mContext,            R.layout.autocomplete_List_item, mLatLng);    mSearchTextVIEw.setAdapter(adapter);    mSearchTextVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        @OverrIDe        public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) {            String str = (String) adapterVIEw.getItemAtposition(i);            initiateSearch();            hIDeKeyboard();        }    });}

activity_map.xml

<autoCompleteTextVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:ID="@+ID/actvSearch"    androID:hint="@string/search_or_enter_address"    androID:background="@color/transparent_white"    androID:textSize="14sp"    androID:textcolor="@color/black"    androID:layout_centerVertical="true"    androID:layout_toEndOf="@ID/iblogoImage"    androID:layout_toStartOf="@ID/ibSearch"    androID:dropDownAnchor="@ID/search_bar"    androID:dropDownVerticalOffset="0dp" />

autocomplete_List_item.xml

<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@color/white"    androID:textcolor="@color/black"    androID:paddingtop="10dp"    androID:paddingBottom="10dp"    androID:paddingStart="25dp"    androID:paddingEnd="25dp"/>

autoCompleteAdapter.java

public class autoCompleteAdapter extends ArrayAdapter<String> implements Filterable {    private static String TAG = "autoComplete";    /*     * The lat/lng of the current location.     */    private LatLng mLatLng;    /*     * A List of the autocomplete results.     */    private ArrayList<String> mResults;    public autoCompleteAdapter(Context context, int textVIEwResourceID, LatLng latLng) {        super(context, textVIEwResourceID);        Log.d(TAG, "Center of Screen: " + latLng.toString());        mLatLng = latLng;    }    @OverrIDe    public int getCount() {        return mResults.size();    }    @OverrIDe    public String getItem(int index) {        return mResults.get(index);    }    @OverrIDe    public Filter getFilter() {        Filter filter = new Filter() {            @OverrIDe            protected FilterResults performFiltering(CharSequence constraint) {                FilterResults filterResults = new FilterResults();                if (constraint != null) {                    // RetrIEve the autocompleteHelper results.                    mResults = autoCompleteHelper.getautoCompletePredictions(                            constraint.toString(),                            mLatLng);                    // Assign the data to the FilterResults                    filterResults.values = mResults;                    filterResults.count = mResults.size();                }                return filterResults;            }            @OverrIDe            protected voID publishResults(CharSequence constraint, FilterResults results) {                if (results != null && results.count > 0) {                    notifyDataSetChanged();                }                else {                    notifyDataSetInvalIDated();                }            }};        return filter;    }}

解决方法:

我实际上可以弄清所有问题:)这是我使用的相关代码.

自定义字体

这个技巧是,我必须在Activity中将字体设置为mautoCompleteTextVIEw和tvautocompleteListItem.

去除阴影

我将mautoCompleteTextVIEw的背景设置为R.drawable.autocomplete_dropdown.在该绘图中,重要的一行是

<stroke    androID:wIDth="0dip"    androID:color="@color/cp_green" />

半径

半径是在R.drawable.autocomplete_dropdown中设置的,如下所示:

    <corners        androID:radius="20dip"/>

MapActivity.java

private voID setautoCompleteListener() {    mautoCompleteTextVIEw.setDropDownBackgroundDrawable(            mContext.getResources().getDrawable(R.drawable.autocomplete_dropdown));    mautoCompleteTextVIEw.setAdapter(            new autoCompleteAdapter(mContext, R.layout.autocomplete_List_item, mLatLng));    mautoCompleteTextVIEw.setonItemClickListener(new AdapterVIEw.OnItemClickListener() {        @OverrIDe        public voID onItemClick(AdapterVIEw<?> adapterVIEw, VIEw vIEw, int i, long l) {            String autoCompleteText = (String) adapterVIEw.getItemAtposition(i);            mautoCompleteTextVIEw.setText(autoCompleteText);            initiateSearch();            hIDeKeyboard();        }    });    mautoCompleteTextVIEw.addTextChangedListener(new TextWatcher() {        @OverrIDe        public voID beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {        }        @OverrIDe        public voID onTextChanged(CharSequence charSequence, int i, int i2, int i3) {        }        @OverrIDe        public voID afterTextChanged(Editable editable) {            if (editable.length() > 0) {                mClearTextIcon.setVisibility(VIEw.VISIBLE);            } else {                mClearTextIcon.setVisibility(VIEw.INVISIBLE);            }        }    });}public voID applyFonts() {    Log.d(TAG, "Applying Fonts.");    FontHelper.applyFont(findVIEwByID(R.ID.rlMap), mContext);    Font = Font.getInstance(getApplicationContext());    mautoCompleteTextVIEw.setTypeface(Font.mAvenirLTStandardlight);    LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);    VIEw vIEw = inflater.inflate(R.layout.autocomplete_List_item, null);    TextVIEw tvautocompleteListItem = (TextVIEw) vIEw.findVIEwByID(R.ID.tvautocompleteListItem);    tvautocompleteListItem.setTypeface(Font.mAvenirLTStandardlight);}

autocomplete_dropdown.xml

<?xml version="1.0" enCoding="UTF-8"?><shape xmlns:androID="http://schemas.androID.com/apk/res/androID">    <solID        androID:color="@color/white" />    <stroke        androID:wIDth="0dip"        androID:color="@color/cp_green" />    <corners        androID:radius="20dip"/>    <padding        androID:left="25dip"        androID:top="10dip"        androID:right="25dip"        androID:bottom="10dip" /></shape>

autocomplete_List_item.xml

<?xml version="1.0" enCoding="utf-8"?><TextVIEw xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@color/white"    androID:textcolor="@color/gray_text"    androID:textSize="14sp"    androID:layout_marginStart="25dp"    androID:layout_marginEnd="25dp"    androID:paddingtop="10dp"    androID:paddingBottom="10dp"    androID:ID="@+ID/tvautocompleteListItem"/>

activity_map.xml

<relativeLayout    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:background="@drawable/search"    androID:ID="@+ID/search"    androID:paddingtop="8dp"    androID:paddingBottom="8dp"    androID:paddingStart="10dp"    androID:paddingEnd="10dp"><relativeLayout    androID:layout_wIDth="match_parent"    androID:layout_height="44dp"    androID:background="@drawable/search_bar"    androID:ID="@+ID/search_bar"><Imagebutton    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:background="@drawable/logo_image"    androID:layout_centerVertical="true"    androID:layout_marginStart="10dp"    androID:layout_marginEnd="0dp"    androID:ID="@+ID/iblogoImage"    androID:contentDescription="@string/logo"/><Imagebutton    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:background="@drawable/search_icon"    androID:layout_centerVertical="true"    androID:layout_marginStart="0dp"    androID:layout_marginEnd="15dp"    androID:layout_alignParentEnd="true"    androID:ID="@+ID/ibSearch"    androID:contentDescription="@string/search_hint"/><Imagebutton    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content"    androID:ID="@+ID/ibClearText"    androID:layout_toStartOf="@ID/ibSearch"    androID:background="@drawable/clear_text"    androID:visibility="invisible"    androID:layout_centerVertical="true"    androID:layout_marginStart="10dp"    androID:layout_marginEnd="20dp"    androID:contentDescription="@string/clear" /><autoCompleteTextVIEw    androID:layout_wIDth="match_parent"    androID:layout_height="wrap_content"    androID:ID="@+ID/actvSearch"    androID:hint="@string/search_or_enter_address"    androID:background="@color/transparent_white"    androID:textSize="14sp"    androID:textcolor="@color/black"    androID:completionThreshold="3"    androID:focusable="true"    androID:focusableIntouchMode="true"    androID:layout_centerVertical="true"    androID:layout_toEndOf="@ID/iblogoImage"    androID:layout_toStartOf="@ID/ibClearText"    androID:dropDownAnchor="@ID/search_bar"    androID:dropDownVerticalOffset="10dp" />    </relativeLayout></relativeLayout>
总结

以上是内存溢出为你收集整理的设置Android AutoComplete的样式全部内容,希望文章能够帮你解决设置Android AutoComplete的样式所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1211577.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存