2013-08-14 40 views
16

Tôi cần thay đổi kích thước kích thước phần này thành hiển thị đầy đủ. Tôi có thể làm cái này như thế nào?Thay đổi chiều rộng Spinner DropDown

Example image

bộ chuyển đổi của tôi:

String[] navigations = getResources().getStringArray(R.array.actionBar); 
     ArrayAdapter<String> adapter = new ArrayAdapter<String>(
       getBaseContext(), R.layout.custom_spinner_title_bar, 
       android.R.id.text1, navigations); 
     adapter.setDropDownViewResource(R.layout.custom_spinner_title_bar); 
     actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); 
     actionBar.setListNavigationCallbacks(adapter, navigationListener); 

custom_spinner_title_bar.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/RelativeLayout1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:gravity="fill_horizontal" 
    android:orientation="vertical" > 

    <TextView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@android:id/text1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:padding="5dip" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#FFFFFF" /> 

</RelativeLayout> 
+0

thử có spinner của bạn chỉ xem trong bố cục bố mẹ nằm ngang của nó. Ý tôi là thử loại bỏ nút màu đỏ bên cạnh nó để xem nó có giúp cho – arianoo

Trả lời

3

Những gì bạn cần làm là sử dụng một bộ chuyển đổi tùy chỉnh cho trình đơn thả xuống thay vì một mặc định , nơi bạn đặt "MinimumWidth" của mỗi 'hàng' thành bất kỳ thứ gì bạn muốn:

private class myCustomAdapter extends ArrayAdapter{ 
    private List<String> _navigations; 
    private int _resource; 
    private int _textViewResourceId; 

    public myCustomAdapter (Context context, int resource, int textViewResourceId, List<String> objects) { 
     super(context, resource, textViewResourceId, objects); 
     _navigations = objects; 
     _resource = resrouce; 
     _textViewResourceId = textViewResourceId; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent){ 
     View row; 
     LayoutInflater inflater=getLayoutInflater();    
     row = inflater.inflate(_resource, null); 
     TextView _textView = row.findViewById(_textViewResourceId); 
     _textView.setText(_navigations.get(position)); 


     Display display = getWindowManager().getDefaultDisplay(); 
     Point size = new Point(); 
     display.getSize(size); 
     int _width = size.x; 

     row.setMinimumWidth = _width; 
     return row; 
    } 
} 

Bạn có thể chơi tất nhiên với "minimumWidth" là bất kỳ điều gì bạn muốn; Trong ví dụ này, nó chỉ được thiết lập để phù hợp với chiều rộng màn hình (mặc dù một cách tiếp cận thông minh hơn sẽ là để đo khung chứa của ứng dụng của bạn và kết hợp nó với điều đó).

Sau đó, để thiết lập adapter:

myCustomAdapter adapter = new myCustomAdapter(getBaseContext(), R.layout.custom_spinner_title_bar,android.R.id.text1, navigations); 
+0

cảm ơn bạn không, nhưng bây giờ tôi không thể chấp nhận câu trả lời của bạn, bởi vì tôi đã từ chối thực hiện điều này – WOLVERINE

+3

Bạn COULD trong lý thuyết nhắm vào chính spinner và usr setDropDownWidth, nhưng vẫn sẽ yêu cầu bộ điều hợp tùy chỉnh và sẽ chỉ hoạt động trên Jellybean trở lên. –

63

Add Attribute trong file xml trong thẻ Spinner

android: dropDownWidth = "150dp"

+0

Rất đơn giản nhưng hiệu quả – Denny

Các vấn đề liên quan