spinner shows like dialog

2019-03-30 05:34发布

问题:

I am using the spinner and it opens like a dialog. I need spinner to be open like dropdown menu.

Here is the xml file

 <Spinner
 android:id="@+id/sp_countrycode_issue_coupon"
 android:layout_width="60dp"
 android:layout_height="40dp"
 android:layout_gravity="center"
 android:layout_marginLeft="3dp"
 android:layout_weight="1"
 android:background="@drawable/img_small_box_green"
 android:dropDownSelector="@drawable/img_small_box_green"
 android:gravity="right|center"
 android:textAlignment="center" />

Here is the code

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
            this, R.array.country_code,
            android.R.layout.simple_spinner_item);

adapter.setDropDownViewResource(R.layout.spinner_textview);
sp_country_code.setAdapter(adapter);

I posted my images bellow like my view and spinner display view. But I need the spinner to be display like the third image

回答1:

For the information Dropdown spinner that you shown in Image 3 is applicable from version 3.0 and above. For previous versions like 2.2 and 2.3 it will open like dialog. It will automatically show dropdown in versions above 2.3

Styles will be automatically added if you choose android version 4.0 or above.

<!--
    Base application theme for API 14+. This theme completely replaces
    AppBaseTheme from BOTH res/values/styles.xml and
    res/values-v11/styles.xml on API 14+ devices.
-->
<style name="AppBaseTheme" parent="android:Theme.DeviceDefault.Light.DarkActionBar">
    <!-- API 14 theme customizations can go here. -->
</style>    

Refrence code

    severitySpinner = (Spinner) findViewById(R.id.spinnerSeverity);
    severityAdapter = ArrayAdapter.createFromResource(this,
            R.array.severity_arrays, android.R.layout.simple_spinner_item);
    severityAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    severitySpinner.setAdapter(severityAdapter);

Edit

<application
        android:icon="@drawable/appicon"
        android:label="Test"
        android:theme="@style/AppTheme" >


回答2:

Late but may be useful to other people, you can simply set the spinnerMode to dropdown:

android:spinnerMode="dropdown"

Else, if you decided you fancied a change and wanted dialog in the future just change the mode to dialog!



回答3:

in manifest file change theme to android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" and in xml file set style for spinner style="@style/MyDropDownNav" and in style file add style.

<!-- style the list navigation -->
<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner">
    <item name="android:popupBackground">@drawable/bg_popup</item>
    <item name="android:dropDownSelector">@drawable/top_layout</item>
</style>


回答4:

Just change the code like this:

ArrayAdapter<String> spinnerMenu = new ArrayAdapter<String>(
                    RegisterActivity.this, R.layout.spinner_item, list);
            // TODO Auto-generated method stub
            spinnerMenu
                    .setDropDownViewResource(android.R.layout.simple_list_item_single_choice);

            country.setAdapter(spinnerMenu);
            country.getSelectedItemPosition();