如何在自定义微调器中隐藏下拉箭头?

时间:2018-10-23 17:39:23

标签: android android-spinner

首先,我使用默认的Spinner来显示Spinner中的数据,但无法更改Spinner的字体样式。因此,我使用了Custom Spinner来显示数据并能够更改Font样式

这是下拉快照ScreenShots的下拉箭头是图片

因此,当我点击下拉菜单时,我希望下拉箭头应隐藏。   这是xml代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:fontFamily="@font/montserrat_regular"
        android:textSize="@dimen/_12sdp"
        android:padding="@dimen/_5sdp"
        android:layout_marginTop="@dimen/_1sdp"
        android:textAlignment="center"
        />
</LinearLayout>

这是活动xml代码

<Spinner
    style="@style/SpinnerTheme"
    android:id="@+id/languageDropdown"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="16dp"
    android:background="@null"
    android:entries="@array/Languages"
    android:spinnerMode="dropdown"
    android:textAlignment="center"
    android:gravity="center"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.501"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/relativeLayout15"

  />

<ImageButton
    android:layout_width="10dp"
    android:layout_height="6dp"
    android:layout_alignParentBottom="true"
    android:layout_marginEnd="8dp"
    android:layout_marginStart="33dp"
    android:layout_marginTop="29dp"
    android:layout_toEndOf="@+id/languageDropdown"
    android:background="@null"
    android:scaleType="fitCenter"
    android:src="@drawable/dropdownarrow"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.589"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/relativeLayout15" />

private class MyArrayAdapter extends BaseAdapter {

private LayoutInflater mInflater;

public MyArrayAdapter(LoginActivity con) {
    // TODO Auto-generated constructor stub
    mInflater = LayoutInflater.from(con);
}

@Override
public int getCount() {
    // TODO Auto-generated method stub
    return spinnerData.length;
}

@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    final ListContent holder;
    View v = convertView;
    if (v == null) {
        v = mInflater.inflate(R.layout.my_spinner_style, null);
        holder = new ListContent();

        holder.name = (TextView) v.findViewById(R.id.textView1);

        v.setTag(holder);
    } else {

        holder = (ListContent) v.getTag();
    }

  //  holder.name.setTypeface(myFont);
    holder.name.setText("" + spinnerData[position]);

    return v;
}



}

static class ListContent {

TextView name;

}

2 个答案:

答案 0 :(得分:0)

创建如下所示的自定义微调器视图,其中包含一个可以监听的侦听器,并在关闭或打开微调器时收到通知。

  

customSpinner类的参考:   https://stackoverflow.com/a/18636385/8551764

public class CustomSpinner extends Spinner {

   /**
    * An interface which a client of this Spinner could use to receive
    * open/closed events for this Spinner. 
    */
    public interface OnSpinnerEventsListener {

        /**
         * Callback triggered when the spinner was opened.
         */
         void onSpinnerOpened(Spinner spinner);

        /**
         * Callback triggered when the spinner was closed.
         */
         void onSpinnerClosed(Spinner spinner);

    }

    private OnSpinnerEventsListener mListener;
    private boolean mOpenInitiated = false;


    public CustomSpinner(Context context) {
        super(context);
    }

    public CustomSpinner(Context context, int mode) {
        super(context, mode);
    }

    @Override
    public boolean performClick() {
        // register that the Spinner was opened so we have a status
        // indicator for when the container holding this Spinner may lose focus
        mOpenInitiated = true;
        if (mListener != null) {
            mListener.onSpinnerOpened(this);
        }
        return super.performClick();
    }

    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        if (hasBeenOpened() && hasFocus) {
            performClosedEvent();
        }
    }

    /**
    * Register the listener which will listen for events.
    */
    public void setSpinnerEventsListener(
            OnSpinnerEventsListener onSpinnerEventsListener) {
        mListener = onSpinnerEventsListener;
    }

    /**
     * Propagate the closed Spinner event to the listener from outside if needed.
     */
    public void performClosedEvent() {
        mOpenInitiated = false;
        if (mListener != null) {
            mListener.onSpinnerClosed(this);
        }
    }

    /**
     * A boolean flag indicating that the Spinner triggered an open event.
     * 
     * @return true for opened Spinner 
     */
    public boolean hasBeenOpened() {
        return mOpenInitiated;
    }

}

然后使用其侦听器,如下所示以隐藏和显示箭头键。为了隐藏和显示微调框,您应该更改自定义微调框的背景。

CustomSpinner customSpinner = new CustomSpinner(context);
final Drawable drawable = customSpinner.getBackground();

        customSpinner.setSpinnerEventsListener(new CustomSpinner.OnSpinnerEventsListener() {
            @Override
            public void onSpinnerOpened(Spinner spinner) {
                spinner.setBackground(null);
            }

            @Override
            public void onSpinnerClosed(Spinner spinner) {
                spinner.setBackground(drawable);
            }
        });

答案 1 :(得分:0)

在Spinner小部件中使用此属性

 android:background="@null"