问题描述
我要选择点击状态,一个简单的布局,以利用其子女的顶部。我认为这是类似于ListView中的drawSelectorOnTop属性。举例布局:
I want a selector click state for a simple layout to draw on top of its children. I think this is similar to the "drawSelectorOnTop" attribute in ListView. Example layout:
<LinearLayout
android:background="@drawable/my_click_selector">
<LinearLayout
android:background="#F00" />
</LinearLayout>
---------------
| |
| ----------- |
| ----------- |
| |
---------------
所以在这里我们有一个父线性布局,其中有一个坚实的背景颜色(红色)内的孩子。当我单击父,对资产的改变背景颜色如在我的选择定义,但由于子更接近在z顺序用户,它保持不变。
So here we have a parent linear layout, and an inner child which has a solid background color (red). When I click the parent, the background color of the asset changes as defined in my selector, but since the child is closer to the user in the z-order, it remains unchanged.
有没有什么办法可以有选择选择的状态画上的所有儿童,而不是在顶部?
Is there any way to have the selector selected-state drawn on top of all children instead of under?
感谢
推荐答案
我面对这个问题,我看着你的问题之前,在这里我做了什么,并完美地工作:
I face this problem and i looked to your question before , here what i did and worked perfectly :
您可以使用的FrameLayout
安卓前景
属性做的工作适合你
You can use the FrameLayout
android:foreground
attribute to do the job for you
样品code
<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
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
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
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>
这篇关于选择绘制在上面 - 一个基本的线性布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!