Android: Change Spinner Dropdown view

2019-02-12 16:10发布

问题:

Im My application I want the below type of Spinner Dropdown view . For this type of spinner view. I wrote this code.

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
            R.array.spinner, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner_obj.setAdapter(adapter);

I got this from http://developer.android.com/guide/topics/ui/controls/spinner.html But what I got is,

Please provide me the best way to do this....

回答1:

Kind of reviving an old post here but the accepted answer is far from ideal. The correct way to do this is to set the Spinner to dropdown mode in your layout xml:

<Spinner 
    android:id="@+id/my_spinner"
    ...
    android:spinnerMode="dropdown"/>

The available options are "dialog" and "dropdown".



回答2:

Your application is running on old theme.

If you are using android 4.2 set android application theme (in the manifest file) to

 android:theme="@android:style/Theme.Holo.Light"

OR

 android:theme="@android:style/Theme.Holo.Light.DarkActionBar"


回答3:

may be you are running in below than 4.0 , 4.0 will show you dropdown as your image



回答4:

You can use popup like below:

           spinner=(EditText)findViewById(R.id.txt_Spinner);


        spinner.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                p = new Point();
                p.x = location[0]+(v.getHeight());
                p.y = location[1]+v.getHeight();

                if (p != null)
                    showPopup(statusActivity.this, p);

                System.out.println("show popup");
            }
        });




    // The method that displays the popup.
    private void showPopup(final Activity context, Point p) {
        int popupWidth = 300;
        int popupHeight = 500;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
        LayoutInflater layoutInflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        // Creating the PopupWindow
        popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);

        // Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
        int OFFSET_X = 00;
        int OFFSET_Y = 00;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
        ((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
        ((TextView)layout.findViewById(R.id.textView9)).setClickable(true);

    }

and popup.xml

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/popup_bg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        style="@style/text_orange_heading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Select Status"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Sleeping"
        android:text="Sleeping" />

    <TextView
        android:id="@+id/textView3"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Available"
        android:text="Available" />

    <TextView
        android:id="@+id/textView4"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Busy"
        android:text="Busy" />

    <TextView
        android:id="@+id/textView5"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At work"
        android:text="At work" />

    <TextView
        android:id="@+id/textView6"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="Battery charge low"
        android:text="Battery charge low" />

    <TextView
        android:id="@+id/textView7"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="In meeting"
        android:text="In meeting" />

    <TextView
        android:id="@+id/textView8"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="TMS me later"
        android:text="TMS me later" />

    <TextView
        android:id="@+id/textView9"
        style="@style/text_blue_contains"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:onClick="onClick"
        android:clickable="true"
        android:drawableBottom="@drawable/line_white"
        android:tag="At the toilet"
        android:text="At the toilet" />

    <EditText
        android:id="@+id/textCustomize"
        style="@style/text_blue_contains"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        android:tag="Customize"
        android:text="Customize" />

</LinearLayout>


回答5:

For the GUI Use HoloEverywhere. https://github.com/Prototik/HoloEverywhere HoloEverywhere is the best way to go if you want Holo theme on older Android then 4.0 .

And for the dropdown use android:spinnerMode="dropdown" in the layout as Stephen Kidson mentioned.



回答6:

Its a AutocompleteTextView widget that you are suppose to use but you have tried with Spinner widget.

If my guess is right, Please refer the link. AutocompleteTextView

Cheers.