Android Spinner下拉视图不透明

时间:2016-09-26 09:29:45

标签: android background spinner

我正在使用Spinner TextView。我将下拉列表背景设置为透明。但是当应用运行时,Spinner只是透明的。下拉视图显示白色。

图片:

enter image description here

main_activity.xml:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:gravity="center"
        android:layout_below="@+id/phone"
        android:spinnerMode="dropdown"
        android:id="@+id/eventspinner"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="30dp"
        android:layout_marginRight="30dp"
/>

spinner_dropdown_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/transparent"
    >

    <TextView
        android:id="@+id/eventText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginLeft="10dp"
        android:text="Text Here"
        android:background="@android:color/transparent"
        android:layout_centerVertical="true">
    </TextView>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.5dp"
        android:layout_marginTop="14dp"
        android:layout_below="@+id/eventText"
        android:background="#90000000"></View>
</RelativeLayout>

CustomAdapter.java:

public class CustomAdapter extends ArrayAdapter<String> {
    public CustomAdapter(Context context, int resource, List objects) {
        super(context, resource, objects);
    }
    public View getCustomView(int position,View convertview,ViewGroup parent){
        LayoutInflater layoutInflater=getLayoutInflater();
        View view= layoutInflater.inflate(R.layout.spinner_dropdown_layout,parent,false);
        view.setBackgroundColor(Color.TRANSPARENT);
        TextView eventText=(TextView) view.findViewById(R.id.eventText);
        eventText.setText((CharSequence) spinnerData.get(position));
        return view;


    }
    @Override
    public View getDropDownView(int position, View convertView,
                                ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }
}

MainActivity.java:

 spinnerData=new ArrayList();
            spinnerData.add("Select from below");
            spinnerData.add("Marriage");
            spinnerData.add("House Warming");
            eventSpinner=(Spinner) findViewById(R.id.eventspinner);
            eventSpinner.setAdapter(new CustomAdapter(this,R.layout.spinner_dropdown_layout,spinnerData));
            eventSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Snackbar.make(main_layout, (CharSequence) parent.getItemAtPosition(position),Snackbar.LENGTH_SHORT).show();
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {
                Snackbar.make(main_layout,"Noting selected",Snackbar.LENGTH_SHORT).show();
                }
            });

color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#ffffff</color>
</resources>

6 个答案:

答案 0 :(得分:4)

您需要设置下拉背景颜色。

ex-

  

机器人:popupBackground =&#34;#COLOR_CODE&#34;

<Spinner
  android:layout_width="match_parent"
  android:layout_height="35dp"
  android:gravity="center"
  android:popupBackground="@android:color/transparent"
  android:layout_below="@+id/phone"
  android:spinnerMode="dropdown"
  android:id="@+id/eventspinner"
  android:layout_marginTop="10dp"
  android:layout_marginLeft="30dp"
  android:layout_marginRight="30dp"
/>

答案 1 :(得分:1)

向微调器添加透明背景

<Spinner
    android:background="@android:color/transparent"
    android:layout_width="match_parent"
    android:layout_height="35dp"
    android:gravity="center"
    android:layout_below="@+id/phone"
    android:spinnerMode="dropdown"
    android:id="@+id/eventspinner"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
/>

答案 2 :(得分:1)

尝试按如下方式设置DropDownViewResource

final CustomAdapter eventSpinnerAdapter = new CustomAdapter(this,R.layout.spinner_dropdown_layout,spinnerData);    
eventSpinner.setAdapter(eventSpinnerAdapter);
eventSpinnerAdapter.setDropDownViewResource(R.layout.spinner_dropdown_layout);

答案 3 :(得分:0)

试试这个

机器人:popupBackground =&#34; @android:彩色/透明&#34;

答案 4 :(得分:0)

这样做会解决你的问题:

 <Spinner
    android:id="@+id/datarenewalperiod_spinner"
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:background="#2eb01e"
    android:popupBackground="@android:color/transparent" />

答案 5 :(得分:0)

您可以在getDropDownView中调整它:

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    if (convertView == null)
    {
        LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.spinner_dropdown_item, null);
    }
    TextView label = (TextView) super.getDropDownView(position, convertView, parent);

    label.getBackground().setAlpha(50);

    return label;
}

取自here

的setAlpha