我有一个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;
}
}