2017-04-14 2 views
0

enter image description hereWie ändert man die Dropdown-Breite des Spinner?


main.xml

<LinearLayout 
    android:id="@+id/spinners" 
    android:layout_width="match_parent" 
    android:layout_height="40dp" 
    android:orientation="horizontal"> 
    <Spinner 
     android:id="@+id/location_spinner" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:dropDownWidth="match_parent"/> 
    <Spinner 
     android:id="@+id/license_spinner" 
     android:layout_width="0dp" 
     android:layout_height="match_parent" 
     android:layout_weight="1" 
     android:dropDownWidth="match_parent"/> 
</LinearLayout> 

spinner_row.xml

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical" 
    android:orientation="horizontal"> 

    <Button 
     android:id="@+id/btn_1" 
     style="@style/SpinnerButton"> 
    </Button> 

</LinearLayout> 

style.xml

<style name="SpinnerButton" parent="@android:style/Widget.Button"> 
    <item name="android:layout_width">match_parent</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:layout_marginLeft">5dp</item> 
    <item name="android:layout_marginRight">5dp</item> 
    <item name="android:layout_marginBottom">10dp</item> 
    <item name="android:textColor">@android:color/black</item> 
    <item name="android:layout_weight">1</item> 
    <item name="android:clickable">true</item> 
</style> 

ich Drop-Down-wie Stil will [Bild B]. Ich habe dropDownWidth = match_parent verwendet. Aber es funktioniert nicht. Ich denke es wegen dieses Elternteils von dropDown ist Spinner. Jeder Spinner hat das Gewicht 1. Gibt es eine Möglichkeit, ein breites Dropdown-Layout anzuwenden?

+0

Try Popup-Menü statt Spinner, Überprüfen Sie bitte diesem Link http://stackoverflow.com/questions/38477208/set-popup-menu-to-full-screen – Sach

+0

@ Sachin Shelar >> Dank für Ihren Kommentar . Ich denke du und @Android Dev empfehlen den gleichen Weg. Recht? – 01hanst

+0

Mögliches Duplikat von [Spinner DropDown Breite ändern] (http://stackoverflow.com/questions/18235711/change-spinner-dropdown-width) –

Antwort

0

Versuchen Sie folgenden Code, wenn es für Sie funktioniert.

DealCategorySpinner = (Spinner) findViewById(R.id.DealCategorySpinner); 

    try { 
     Field popup = Spinner.class.getDeclaredField("mPopup"); 
     popup.setAccessible(true); 

     // Get private mPopup member variable and try cast to ListPopupWindow 
     android.widget.ListPopupWindow popupWindow = (android.widget.ListPopupWindow) popup.get(DealCategorySpinner); 

     // Set popupWindow width to 800px 
     popupWindow.setWidth(800); 
    } 
    catch (NoClassDefFoundError | ClassCastException | NoSuchFieldException | IllegalAccessException e) { 
     e.printStackTrace(); 
    } 
+0

danke, ich werde es versuchen – 01hanst

Verwandte Themen