5

Tôi cố gắng để áp dụng hiệu ứng gợn trên mặt thả xuống Spinner, như thế này:hiệu lực Ripple vào các mặt hàng thả xuống Spinner màu nền (appcompat-v7 V21)

activity.java

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
      this, R.array.array_name, R.layout.simple_spinner_dropdown_item); 
    adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item); 
    Spinner mSpinner = (Spinner) findViewById(R.id.spinner); 
    mSpinner.setAdapter(adapter); 

simple_spinner_dropdown_item. xml

<?xml version="1.0" encoding="utf-8"?> 
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/spinner_ripple" 
    android:ellipsize="marquee" 
    android:gravity="center_horizontal" 
    android:padding="10dip" 
    android:singleLine="true" 
    android:textSize="14sp" /> 

spinner_ripple.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item> 
    <ripple android:color="?android:attr/colorControlHighlight"> 
     <item><shape> 
        <solid android:color="@android:color/white" /> 
       </shape> 
     </item> 
    </ripple> 
</item> 
</selector> 

nhưng trong danh sách thả xuống nó hoạt động chỉ dành cho các mục đầu tiênchỉ khi mục đã chọn hiện tại là một trong những khác so với đầu tiên. Trong tất cả các trường hợp khác, nó điền vào mục nền với màu gợn sóng (không có hiệu ứng gợn sóng). Mã của tôi ở đâu sai?

Đã cố gắng: doesnt làm việc để thiết lập màu sắc cố định trên nền tảng của bố cục mục thả xuống và di chuyển hiệu ứng gợn dưới phần tử Spinner, như thế này:

simple_spinner_dropdown_item.xml

<?xml version="1.0" encoding="utf-8"?> 
<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@android:color/white" 
    android:ellipsize="marquee" 
    android:gravity="center_horizontal" 
    android:padding="10dip" 
    android:singleLine="true" 
    android:textSize="14sp" /> 

activity_layout .xml

<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:drawSelectorOnTop="true" 
    android:dropDownSelector="@drawable/spinner_ripple" /> 

Trả lời

3

Cách tiếp cận thứ hai của bạn là chính xác, nhưng như đã đề cập trong thuộc tính this question, android:dropDownSelector không hoạt động và đó là lỗi phổ biến.

Ngoài ra, nếu bạn muốn thay đổi màu nền thả xuống, bạn cần đặt Spinner'sandroid:popupBackground, thay vì đặt nền của mục thả xuống.

Toàn bộ giải pháp sẽ là như thế này:

popup_background.xml

<!--Based on Android's popup_background_material.xml--> 
<!--This changes the background of the drop-down--> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
     android:shape="rectangle"> 
    <corners android:radius="2dp" /> 
    <solid android:color="@android:color/white" /> 
</shape> 

styles.xml

<resources> 
    <style name="AppTheme" parent="android:Theme.Material.Light"> 
     <!--Workaround for the Android bug--> 
     <item name="android:dropDownListViewStyle">@style/Theme.MyListView</item> 
    </style> 

    <style name="Theme.MyListView" parent="@android:style/Widget.Material.ListView.DropDown"> 
     <item name="android:listSelector">@drawable/spinner_ripple</item> 
    </style> 
</resources> 

activity.xml

... 
<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:dropDownSelector="@drawable/spinner_ripple" 
    android:popupBackground="@drawable/popup_background" 
    /> 
... 

spinner_ripple.xml (bản gốc)

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <ripple android:color="?android:attr/colorControlHighlight"> 
      <item> 
       <shape> 
        <solid android:color="@android:color/white" /> 
       </shape> 
      </item> 
     </ripple> 
    </item> 
</selector> 
+0

Bằng cách này hiệu ứng gợn sóng trên nền trắng được di chuyển trên cấp chủ đề: nó hoạt động, nhưng hoạt động cho tất cả các spinners trong ứng dụng của tôi. Vì vậy, tôi không thể có một spinner duy nhất với hiệu ứng gợn trên màu nền riêng của mình khác với chủ đề một. – GPack

+0

Đó là điều không may nhưng do lỗi Android là cách duy nhất, trừ khi bạn muốn phân lớp lớp spinner và cố gắng triển khai một giải pháp thay thế trong đó. – stealth

+0

Btw, tại sao bạn muốn có một spinner khác với những người khác? – stealth

0

Tôi tin rằng bạn cần hai lớp trong của bạn spinner_ripple.xml

<!-- Background --> 
<ripple android:color="#ff00ff00"> 
    <item android:drawable="@android:color/black" /> 
<ripple /> 

<!-- Ripple color --> 
<ripple android:color="#ff00ff00"> 
    <item android:drawable="@android:color/white" /> 
<ripple /> 
+1

Hai 'yếu tố ripple' cùng cấp trong xml dưới 'selector'? Tôi sẽ thử, nhưng tôi nghĩ đó không phải là cách đúng đắn. Cảm ơn. – GPack

4

Nếu bạn muốn có các mục danh sách thả xuống của bạn để có màu trắng theo mặc định và nhấn mạnh họ với hiệu ứng gợn duy nhất khi người dùng chạm vào chúng, bạn cần phải thực hiện trạng thái thích hợp danh sách trong spinner_ripple.xml drawable:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true"> 
     <ripple android:color="?android:attr/colorControlHighlight" /> 
    </item> 
    <item android:state_pressed="false"> 
     <shape> 
      <solid android:color="@android:color/white" /> 
     </shape> 
    </item> 
</selector> 

ở đây, android:state_pressed="true" có nghĩa là mục chọn chỉ được áp dụng khi người dùng thực sự nhấn xem. Nếu bạn muốn hỗ trợ các trạng thái chọn khác, hãy xem tham chiếu StateListDrawable.

Hãy ghi nhớ rằng AppCompat-v7 v21 không hỗ trợ hiệu ứng gợn sóng, theo quan chức Android Developers blog:

Tại sao không có gợn sóng trên mặt trước Lollipop? Rất nhiều thứ cho phép RippleDrawable chạy trơn tru là RenderThread mới của Android 5.0. Để tối ưu hóa hiệu suất trên các phiên bản Android trước, chúng tôi đã rời khỏi RippleDrawable ngay bây giờ.

Vì vậy, nó sẽ chỉ hoạt động trên Lollypop.

+0

Bằng cách này, khi mục được nhấn nền trắng dosnt tồn tại và nó thay đổi màu nền chủ đề cho danh sách thả xuống. – GPack

+0

Phải, hãy xem câu trả lời khác của tôi. Điều đó giải quyết tất cả các vấn đề. – stealth

+0

'state_pressed =" false "' thuộc tính dường như dư thừa trong mục thứ hai. –

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