样式化Android自动完成

时间:2015-03-30 20:30:43

标签: android autocompletetextview

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

1 个答案:

答案 0 :(得分:4)

我能够实际解决所有问题:)以下是我使用的相关代码。

自定义字体

这个的诀窍是我必须在我的Activity中将我的字体设置为mAutoCompleteTextViewtvAutocompleteListItem

删除阴影

我将mAutoCompleteTextView的背景设置为R.drawable.autocomplete_dropdown。在那个drawable中,重要的一行是

<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>