Toolbar Spinner Dropdown inflating issue

时间:2016-04-15 11:11:37

标签: android android-layout android-spinner

I am passing my custom layout in constructor of ArrayAdapter<String>:

private static class ActionSpinnerAdapter extends ArrayAdapter<String> {

   public ActionSpinnerAdapter(final Context context) {
        super(context,
                R.layout.action_spinner_text, //This is my own layout
                //R.id.action_text,
                Lists.newArrayList(ACTION_REPLY, ACTION_REPLY_ALL, ACTION_FORWARD));

action_spinner_text.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/action_text"
  android:layout_width="90dp"
  android:layout_height="wrap_content"
  android:paddingBottom="10dp"
  android:paddingLeft="8dip"
  android:paddingStart="8dp"
  android:paddingTop="10dp"
  android:paddingRight="8dp"
  android:paddingEnd="8dp"
  android:text="@string/reply_all_action"
  android:textSize="18sp" />

And these are my getView() and getDropDownView() methods:

@Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        View result = super.getDropDownView(position, convertView, parent);
        result.setBackgroundColor(mContext.getResources().getColor(R.color.white));
        TextView textView = ((TextView) result.findViewById(R.id.action_text));
        textView.setTextColor(mContext.getResources().getColor(R.color.dark_grey));
        textView.setText(getDisplayValue(position));
        return result;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View result = super.getView(position, convertView, parent);
        TextView textView = ((TextView) result.findViewById(R.id.action_text));
        textView.setTextColor(mContext.getResources().getColor(R.color.white));
        textView.setText(getDisplayValue(position));
        return result;
    }

But When I click on DropDown from Toolbar it shows text values with black background on right side.

enter image description here

What went wrong?

1 个答案:

答案 0 :(得分:0)

我遇到了完全相同的问题,当您在下拉视图中指定的尺寸不是&#34; match_parent&#34;时,看起来预装棒棒糖设备中存在一个错误。 on&#34; android:layout_width&#34;,基本上你需要做的就是 这样:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/action_text"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:paddingBottom="10dp"
  android:paddingLeft="8dip"
  android:paddingStart="8dp"
  android:paddingTop="10dp"
  android:paddingRight="8dp"
  android:paddingEnd="8dp"
  android:text="@string/reply_all_action"
  android:textSize="18sp" />

希望它有帮助!