如何去除 Spinner DropDownView 的阴影

How to remove the Shadow of the Spinner DropDownView

我在 spinnerMode 下拉列表中使用带有自定义适配器和自定义视图的小部件 Spinner。我的问题是我无法删除投射 Spinner 下拉列表的默认阴影。

这里是Activity

里面的代码
mSpinner = (Spinner)findViewById(R.id.spinner);
    setSpinnerData();

    customSpinnerAdapter = new CustomSpinnerAdapter(this,R.layout.spinner_item,CustomListViewValuesArr );
    mSpinner.setAdapter(customSpinnerAdapter);
    mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            itemSelect(pos);
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

这也是 ActivityLayout 内的 Spinner:

 <Spinner
            android:id="@+id/spinner"
            android:layout_centerInParent="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:spinnerMode="dropdown"
            android:background="@null"
            android:padding="0dp"
            android:layout_margin="15dp"
            android:dropDownVerticalOffset="@dimen/vertical_offset"/>

我使用自己的习惯 Adapter :

public class CustomSpinnerAdapter extends ArrayAdapter {
public ArrayList CustomListViewValuesArr;
LayoutInflater inflater;
Typeface myTypeFace;
SpinnerItem item = null;

public CustomSpinnerAdapter(Context context, int textViewResourceId,  ArrayList CustomListViewValuesArr) {
    super(context, textViewResourceId, CustomListViewValuesArr);
    this.CustomListViewValuesArr = CustomListViewValuesArr;
    this.inflater   = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.myTypeFace = Typeface.createFromAsset(context.getAssets(),"fonts/OpenSans-Regular.ttf");
}

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

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

public View getCustomDropDownView(int position, View convertView, ViewGroup parent) {
    Log.d("","CustomListViewValuesArr" + position);
    /********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
    View row = inflater.inflate(R.layout.simple_spinner_dropdown_item, parent, false);
    item = (SpinnerItem) CustomListViewValuesArr.get(position);
    /***** Get each Model object from Arraylist ********/
    TextView label = (TextView)row.findViewById(R.id.spinner_drop_down);
    // Default selected Spinner item
    label.setText(item.getFilterName());
    label.setTypeface(myTypeFace);
    return row;
}

public View getCustomView(int position, View convertView, ViewGroup parent) {
    Log.d("","CustomListViewValuesArr" + position);
    /********** Inflate spinner_rows.xml file for each row ( Defined below ) ************/
    View row = inflater.inflate(R.layout.spinner_item, parent, false);
    item = (SpinnerItem) CustomListViewValuesArr.get(position);
    /***** Get each Model object from Arraylist ********/
    TextView label = (TextView)row.findViewById(R.id.spinner_item);
    TextView hint = (TextView)row.findViewById(R.id.spinner_hint);
    hint.setTypeface(myTypeFace);
    // Default selected Spinner item
    label.setText(item.getFilterName());
    label.setTypeface(myTypeFace);
    return row;
}
}

最后我为下拉列表使用自定义视图

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:elevation="-6dp"
android:layout_height="35dp">
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/spinnerDropDownItemStyle"
        android:singleLine="true"
        android:id="@+id/spinner_drop_down"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="@color/spinner_font_color"
        android:layout_marginLeft="20dp"
        android:textSize="14sp"
        android:layout_marginStart="20dp"
        android:ellipsize="marquee">
    </TextView >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/color_hint_change_pass"/>

以及 Spinner 项目

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:ellipsize="marquee"
android:background="@drawable/spinners_background"
style="@style/spinnerItemStyle"
android:layout_height="35dp">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/spinner_hint"
    android:gravity="center"
    android:textStyle="bold"
    android:textColor="@color/spinner_font_color"
    android:textSize="14sp"
    android:id="@+id/spinner_hint"
    android:layout_marginRight="5dp"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="20dp"
    android:layout_marginStart="20dp"
    android:layout_alignTop="@+id/spinner_item" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/spinner_item"
    android:textColor="@color/spinner_header_color"
    android:gravity="center"
    android:text="sss"
    android:textSize="14sp"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/spinner_hint"
    android:layout_toEndOf="@+id/spinner_hint" />

<ImageView
    android:layout_width="25dp"
    android:layout_height="25dp"
    android:src="@drawable/down_arrow_spinner"
    android:layout_marginRight="17dp"
    android:layout_marginEnd="1dp"
    android:layout_centerVertical="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/imageView2" />

我尝试过但没有奏效的方法:

提前致谢!

最终,我使用了微调器的属性 android:popupBackground="@null",微调器弹出窗口的背景被移除,阴影也被移除。我唯一需要更改的是在我的自定义微调器下拉项中分配 #fff 背景。所以我这样改了:

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="35dp">
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        style="@style/spinnerDropDownItemStyle"
        android:singleLine="true"
        android:id="@+id/spinner_drop_down"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:textColor="@color/spinner_font_color"
        android:layout_marginLeft="20dp"
        android:textSize="14sp"
        android:layout_marginStart="20dp"
        android:ellipsize="marquee">
    </TextView >

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@color/color_hint_change_pass"/>
</RelativeLayout >

使用这个:

android:popupElevation="0dp"