自定义autocompletetextview下拉背景

时间:2018-06-12 10:04:29

标签: android customization autocompletetextview

如何自定义此图像的autocompletetextview下拉背景

this image

目前我正在使用recyclerView和自定义适配器进行过滤,但我需要查看并隐藏recyclerview,这样才能解决为什么我找到自定义autocompletetextview下拉列表的解决方案的原因背景,任何帮助都可以感激,谢谢你提前

2 个答案:

答案 0 :(得分:0)

您可以使用以下功能更改下拉列表的背景 setDropDownBackgroundResource();

答案 1 :(得分:0)

以下是我使用的相关代码。

自定义字体

这个的诀窍是我必须在我的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>