2012-05-23 21 views
8

Tôi muốn có trạng thái nhấp chọn cho một bố cục đơn giản để vẽ lên trên con của nó. Tôi nghĩ rằng điều này tương tự như thuộc tính "drawSelectorOnTop" trong ListView. Bố cục mẫu:Vẽ bộ chọn ở trên cùng - cho bố cục tuyến tính cơ bản?

<LinearLayout 
    android:background="@drawable/my_click_selector"> 
    <LinearLayout 
     android:background="#F00" /> 
</LinearLayout> 

--------------- 
|    | 
| ----------- | 
| ----------- | 
|    | 
--------------- 

Vì vậy, ở đây chúng tôi có bố cục tuyến tính gốc và con bên trong có màu nền rắn (màu đỏ). Khi tôi nhấp vào phụ huynh, màu nền của nội dung thay đổi như được xác định trong bộ chọn của tôi, nhưng vì con gần người dùng theo thứ tự z, nên nó vẫn không thay đổi.

Có cách nào để chọn trạng thái đã chọn được chọn ở trên cùng của tất cả trẻ em thay vì dưới không?

Cảm ơn

Trả lời

14

tôi phải đối mặt với vấn đề này và tôi nhìn vào câu hỏi của bạn trước đây, ở đây những gì tôi đã làm và làm việc một cách hoàn hảo:

Bạn có thể sử dụng thuộc tính FrameLayoutandroid:foreground để làm công việc cho bạn

mã mẫu

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:clickable="true" 
     android:descendantFocusability="afterDescendants" 
     android:duplicateParentState="true" 
     android:focusable="true" 
     > 

     <FrameLayout 
      xmlns:android="http://schemas.android.com/apk/res/android" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:clickable="true" 
      android:descendantFocusability="beforeDescendants" 
      android:focusable="true" 
      android:focusableInTouchMode="true" 
      android:foreground="@drawable/draw_on_top_selector" > 
       <RelativeLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 

     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:background="#282828" 
     android:paddingBottom="5dp" > 



     <ImageView 

      android:layout_width="160dp" 
      android:layout_height="90dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_gravity="center_vertical|center_horizontal" 
      android:adjustViewBounds="true" 
      android:contentDescription="@string/app_name" 
      android:scaleType="centerCrop" 
      android:src="@drawable/video_blank" /> 

     <TextView 

      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@id/episodeImage" 
      android:layout_alignLeft="@id/episodeImage" 
      android:background="@android:color/black" 
      android:gravity="right" 
      android:padding="2dp" 
      android:textColor="#FFFFFF" 
      android:textSize="11sp" /> 

    </RelativeLayout> 

</FrameLayout> 
</LinearLayout> 

draw_on_top_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 


    <item android:drawable="@drawable/show_cell_background_selected" android:state_pressed="true"/> 
    <item android:drawable="@drawable/show_cell_background"/> 

</selector> 

show_cell_background_selected.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <shape android:shape="rectangle" > 
      <stroke 
       android:width="4dp" 
       android:color="#FFD900" > 
      </stroke> 

      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

</layer-list> 

show_cell_background.xml

<?xml version="1.0" encoding="UTF-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > 

    <item> 
     <shape android:shape="rectangle" > 
      <stroke 
       android:width="2dp" 
       android:color="#20000000" > 
      </stroke> 

      <solid android:color="@android:color/transparent" /> 
     </shape> 
    </item> 

</layer-list> 
4

Gần đây tôi made a post về vẽ một chọn foreground trên bất kỳ quan điểm. Nó sẽ cho phép bạn tránh bố trí lồng nhau.

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

+0

Cheers CFlex, chỉ cần sử dụng cách tiếp cận của bạn và các nhà thiết kế đòi hỏi rất hạnh phúc! +1 – OceanLife

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