2015-10-27 20 views
13

Làm cách nào để chỉ định cho một spinner trong Android một phần đệm bổ sung trên đầu/cuối của mục đầu tiên/cuối cùng? Xem mẫu của Goolge bên dưới (khoảng cách 8 dp). Google Simple MenuĐệm trên cùng/dưới cùng cho Spinner

+0

Hãy thử các liên kết này [liên kết thứ 1] (http://stackoverflow.com/questions/17407626/custom-layout-for-spinner-item) [liên kết thứ 2] (http://stackoverflow.com/questions/8946893/ android-custom-spinner-layout) [Chương trình mẫu] (http://mrbool.com/how-to-customize-spinner-in-android/28286) Hy vọng điều này Trợ giúp đầy đủ –

Trả lời

3

Spinner không hỗ trợ nhiều loại chế độ xem. Issue.

Tôi đề xuất bạn sử dụng hộp thoại để hiển thị danh sách của bạn.

Edited

theo nhận xét của bạn giải quyết vấn đề của bạn với điều này question

+0

Giải pháp duy nhất có vẻ là giải pháp này. Tôi tìm thấy nó sau liên kết của bạn.http://stackoverflow.com/a/16400899/519790 – multiholle

2

Bạn cần tạo bộ điều hợp tùy chỉnh cho mình Spinner bằng bố cục tùy chỉnh của mình. Do đó, bạn chỉ cần cung cấp bố cục với lề/pad phù hợp.

Spinner spinner = (Spinner) findViewById(R.id.myspinner); 
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.data, R.layout.spinner_item); 
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item); 
spinner.setAdapter(adapter); 

Bạn có thể tìm thấy một ví dụ hợp lệ here.

1

Chỉ cần tạo bố cục riêng cho các mục bạn muốn hiển thị trong hộp xoay tròn.

Something như thế này

spinner_text_view.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/customTextView" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:padding="15dp" 
android:text="@string/sample_list_item" 
android:textColor="@android:color/black" 
android:textSize="20sp" /> 

Sau đó, trong hoạt động của bạn/mảnh, bạn có thể tạo một bộ chuyển đổi (adapter mảng để đơn giản trong trường hợp này) và thông qua cách bố trí này với tham chiếu bộ điều hợp. Cuối cùng, đặt bộ điều hợp đó cho bộ xoay và bạn có thể nhận được kết quả mong muốn.

Mã mẫu.

private void setupSpinner() { 
    String[] sampleSpinnerItems = {"One", "Two", "Three", "Four", "Five"}; 
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, 
      R.layout.spinner_text_view, sampleSpinnerItems); 
    sampleSpinner.setAdapter(spinnerAdapter); 
}  

Hy vọng điều này sẽ hữu ích.

Lưu ý: spinner_text_view là tệp bố cục bạn đã tạo trước đó.

+0

Điều này không cho tôi thêm đệm trước đầu tiên và sau mục cuối cùng. – multiholle

1

Bạn cần bộ điều hợp tùy chỉnh cho bố cục này và bố cục tùy chỉnh. Bạn có thể sử dụng 2 bố cục. Giả sử bạn có n phần tử để hiển thị, trong spinner của bạn, bạn sẽ có n + 2. iF vị trí là 0 (phần tử đầu tiên) hoặc phần tử cuối cùng bạn sẽ hiển thị bố trí trống nếu không bạn sẽ hiển thị bố cục khác. Bố cục trống bạn sẽ sử dụng để tạo không gian này

1

Bạn có thể thực hiện điều này sử dụng tùy chỉnh Adapter cho Spinner. Nó sẽ giống như thế này:

class YourAdapter extends BaseArrayAdapter<YourObject> { 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = inflater.inflate(R.layout.your_item, parent, false); 
      DropDownViewHolder holder = new DropDownViewHolder(); 
      holder.root = convertView.findViewById(R.id.drop_down_root); 
      // other assignments etc. 
      convertView.setTag(holder); 
     } 
     DropDownViewHolder holder = (DropDownViewHolder) convertView.getTag(); 
     int bottomPadding = 0; 
     int topPadding = 0; 
     if (position == 0) { 
      topPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8); 
     } else if (position == (getCount() - 1)) { 
      bottomPadding = getContext().getResources().getDimensionPixelSize(R.dimen.margin_8); 
     } 

     holder.root.setPadding(0, topPadding, 0, bottomPadding); 
     // other UI related logic etc. 
     return convertView; 
    } 

    // ... 

    public static class DropDownViewHolder { 
     View root; 
     // other views 
    } 
} 

Trên thực tế logic với thiết extra padding có thể được thay đổi để thay đổi một số quan điểm tầm nhìn còn sơ khai để VISIBLE hoặc lạm phát một số bố trí khác tùy thuộc vào vị trí, nhưng giải pháp với đệm có vẻ tự nhiên hơn với tôi.

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