自定义自动完成文本视图下拉背景
customise autocompletetextview drop-down background
如何像这张图片一样自定义 autocompletetextview
下拉背景
目前我正在使用 recyclerView
和可过滤的自定义适配器,但我需要可见和隐藏 recyclerview 并且它很难管理,这就是为什么我找到自定义 autocompletetextview
下拉背景的解决方案,任何帮助可以欣赏,提前谢谢你
您可以使用以下功能更改下拉菜单的背景
setDropDownBackgroundResource();
这是我使用的相关代码。
自定义字体
这个技巧是我必须在 Activity.
中将字体设置为 mAutoCompleteTextView 和 tvAutocompleteListItem
删除阴影
我把mAutoCompleteTextView的背景设置为R.drawable.autocomplete_dropdown。在那个drawable中重要的一行是
<stroke
android:width="0dip"
android:color="@color/cp_green" />
半径
半径在 R.drawable.autocomplete_dropdown 中设置如下:
<corners
android:radius="20dip"/>
MainActivity.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_main.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>
如何像这张图片一样自定义 autocompletetextview
下拉背景
目前我正在使用 recyclerView
和可过滤的自定义适配器,但我需要可见和隐藏 recyclerview 并且它很难管理,这就是为什么我找到自定义 autocompletetextview
下拉背景的解决方案,任何帮助可以欣赏,提前谢谢你
您可以使用以下功能更改下拉菜单的背景 setDropDownBackgroundResource();
这是我使用的相关代码。
自定义字体
这个技巧是我必须在 Activity.
中将字体设置为 mAutoCompleteTextView 和 tvAutocompleteListItem删除阴影
我把mAutoCompleteTextView的背景设置为R.drawable.autocomplete_dropdown。在那个drawable中重要的一行是
<stroke
android:width="0dip"
android:color="@color/cp_green" />
半径
半径在 R.drawable.autocomplete_dropdown 中设置如下:
<corners
android:radius="20dip"/>
MainActivity.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_main.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>