2012-12-04 47 views
21

Tôi đang cố gắng để ứng dụng Android của mình phong cách hơn một chút và đã thực hiện một số tiến bộ nhưng trình đơn thả xuống sẽ khiến tôi gặp rắc rối. Tôi đã có một ảnh chụp màn hình để hiển thị cho bạn những vấn đề:Phong cách android spinner

How do I get rid of the white background

Những gì tôi muốn hộp trắng phía sau để được minh bạch, như trong lớp phủ màu xám cùng trên màn hình trở lại như phần còn lại của là màn hình bên ngoài menu thả xuống.

Android 4.0, API 15 nếu tôi nhớ chính xác.

Theo yêu cầu, đây là cách tôi thực hiện cho đến thời điểm này. Đây là những đoạn tôi nghĩ là có liên quan nhưng tôi có thể đã bỏ lỡ một cái gì đó.

Đây là xml cho spinner:

<Spinner 
    android:id="@+id/edit_countrySpinner" 
    android:layout_width="0dip" 
    android:layout_height="wrap_content" 
    android:layout_weight="1" 
    android:entries="@array/edit_countryArray" 
    android:gravity="center" 
    android:prompt="@string/country_prompt" /> 

Trong các giá trị của tôi/style.xml. Nếu tôi thay đổi màu nền ở đây, nền trong hộp thoại sẽ thay đổi, nhưng tất cả phần còn lại của hình nền là tốt. Tôi đã không tìm ra cách để jus thay đổi nền trong hộp thoại thả xuống.

<style name="appcin" parent="@android:style/Theme.NoTitleBar.Fullscreen"> 
    <item name="android:spinnerStyle">@style/spinnerStyle</item> 
    <item name="android:spinnerItemStyle">@style/SpinnerItem</item> 
    <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item> --> 
    <item name="android:background">#FFFFFF</item> 
</style> 
<style name="spinnerStyle"> 
    <item name="android:background">@drawable/pink_white_dropdown</item> 
    <item name="android:clickable">true</item> 
</style> 
<style name="SpinnerItem"> 
    <item name="android:textColor">#993399</item> 
    <item name="android:background">@drawable/pink_white_dropdown</item> 
    <item name="android:maxHeight">10dip</item> 
</style> 
<style name="SpinnerDropDownItem"> 
    <item name="android:textColor">#993399</item> 
    <item name="android:background">#FFFFFF</item> 
</style> 

Tôi đã thử thêm chủ đề này vào chủ đề ứng dụng nhưng không ai trong số chúng tạo ra bất kỳ sự khác biệt nào, nền vẫn màu trắng.

<...theme....> 
    <item name="android:dropDownListViewStyle">@style/DropDownStyle</item> 
    <item name="android:dropDownSpinnerStyle">@style/DropDownStyle</item> 
    <item name="android:dropDownSelector">@style/DropDownStyle</item> 
</... theme ...> 
<style name="DropDownStyle"> 
    <item name="android:background">#FFF000</item> 
    <item name="android:popupBackground">#FFF000</item> 
    <item name="android:cacheColorHint">#FFF000</item> 
</style> 

Trong drawable/pink_white_dropdown, chỉ hiển thị cùng một hình ảnh cho tất cả các trường hợp. pink_white_arrow là hình ảnh 9patch tôi đã tạo. Có rất nhiều hướng dẫn, here's one I found by 30sec on google.

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_window_focused="false" android:state_enabled="true" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_window_focused="false" android:state_enabled="false" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_pressed="true" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_pressed="false" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_focused="true" android:state_enabled="true" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_enabled="true" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item android:state_focused="true" 
     android:drawable="@drawable/pink_white_arrow"/> 
    <item 
     android:drawable="@drawable/pink_white_arrow"/> 
</selector> 

Một nơi nào đó trong những tệp này, tôi cần phải minh bạch một số thứ, nhưng tôi không biết ở đâu.

+1

Bạn có thể chia sẻ cách bạn làm điều đó cho đến nay? – fiddler

+0

Thêm giải pháp hiện tại, tôi nghĩ rằng _should_ là tất cả những thứ liên quan nhưng tôi không chắc chắn. – dutt

Trả lời

48

Xóa thuộc tính nền SpinnerStyle.

Remove này:

<style name="spinnerStyle"> 
    <item name="android:background">@drawable/whitish_rectangle</item> 
</style> 

đó là những gì thu hút các hình chữ nhật màu trắng nền.

Xây dựng trên mã trong câu hỏi của bạn, đây là một ví dụ cơ bản về cách bạn có thể tạo kiểu Spinner của bạn:

File styles.xml đặt phong cách cho SpinnerItemSpinnerDropDownItem:

<resources> 
    <style name="customtheme" parent="@android:style/Theme.Light"> 
     <item name="android:spinnerItemStyle">@style/SpinnerItem</item> 
     <item name="android:spinnerDropDownItemStyle">@style/SpinnerDropDownItem</item> 
    </style> 
    <style name="SpinnerItem"> 
     <item name="android:textColor">#993399</item> 
     <item name="android:background">@drawable/my_rectangle</item> 
    </style> 
    <style name="SpinnerDropDownItem"> 
     <item name="android:textColor">#993399</item> 
     <item name="android:background">@drawable/my_rectangle</item> 
    </style> 
</resources> 

Để thử nghiệm, Tôi đã tạo một hình dạng có thể vẽ màu sáng, được gọi là my_rectangle.xml.Thay thế này với drawable của riêng bạn:

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" > 
    <solid android:color="#ff0000" /> 
    <stroke 
     android:width="1dp" 
     android:color="#888888" /> 
</shape> 

Thêm Spinner đến Activity's bố trí:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="40dip"> 
    <Spinner 
     android:id="@+id/edit_countrySpinner" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:entries="@array/location_names"/> 
</LinearLayout> 

này tạo ra:

enter image description here

không có hình vuông màu trắng ở chế độ nền.

+0

Rất gần, đó là nền trong chủ đề của tôi đã tạo ra hộp màu trắng phía sau thực sự. Nhưng bạn đã dẫn tôi xuống con đường bên phải và bây giờ nó có vẻ như nó nên. Vâng, ít nhất là bit này:} Bây giờ để tạo kiểu cho các hộp thoại ... Cảm ơn rất nhiều sự giúp đỡ. – dutt

+0

Vui vì nó đã giúp :) –

1

thêm

<item name="android:popupBackground">@null</item> 

phong cách spinner của bạn.

+0

Không hoạt động. Tôi cũng đã thử thêm dòng đó vào tất cả các kiểu có liên quan đến trình quay tròn và chủ đề ứng dụng nhưng điều đó cũng không hoạt động. – dutt

+0

Điều này đã giúp tôi tìm ra giải pháp. Các spinner đã được đặt trên một gradient màu xám xem và vì vậy tôi chỉ muốn nền của thả xuống được grey ánh sáng - nhưng nó đã được mặc định để holo màu tối. Vì vậy, tôi đặt phong cách của spinner thành ​​# DDDDDD và nó hoạt động hoàn hảo. Cảm ơn @faylon! – bkurzius

+1

Bạn có thể giải thích cách bạn đặt kiểu xoay tròn không? Tôi sử dụng điều này trong chủ đề @ style/SpinnerItemAppTheme, trong đó . Nhưng khi tôi cố gắng để thêm các mục bạn đề nghị để SpinnerItemAppTheme không có gì xảy ra '. – Sandra

0

tôi đã cùng một vấn đề, và mã dưới đây làm việc cho tôi:

<item name="android:popupBackground">@null</item> 
Các vấn đề liên quan