如何删除Spinner DropDownView的阴影

时间:2016-08-01 20:28:58

标签: android android-spinner

我将小部件Spinner与自定义适配器和spinnerMode下拉列表中的自定义视图一起使用。我的问题是我无法移除投射Spinner下拉列表的默认阴影。 enter image description here

以下是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) {

        }
    });

这也是Layout的{​​{1}}内的微调器:

Activity

我使用自己的自定义 <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

最后我使用Dropdown的自定义视图

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" /> 小部件
  • 中将高程添加到0dp
  • 向Spinner添加空背景

提前致谢!

2 个答案:

答案 0 :(得分:2)

使用此:

android:popupElevation="0dp"

答案 1 :(得分:0)

最终我使用了一个微调器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 >