视图从其父级获取其可绘制状态(按下,等...) [英] View gets its drawable state (pressed, etc...) from its parent

查看:101
本文介绍了视图从其父级获取其可绘制状态(按下,等...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ListView,其中每个列表项是基于RelativeLayout的自定义布局,它是可点击的,它有一个自定义选择器。 En plus,它有一个sub-RelativeLayout也是可以点击的,它有自己的选择器。



这样的东西:

  --------------- 
| |
| ___ |
| | | |
---------------

一切正常完美但问题是,当我点击父相对布局,子选择器假设父选择器的状态。我尝试设置

  android:duplicateParentState =false

但是没有改变。



任何想法?提前感谢

解决方案

您能否显示ListView的项目和选择器的代码?在我看来,你犯了一些错误,当它被实施。这里是我的项目及其选择器的例子。它适用于我:



视频,我的实现如何工作> click to open<



list_view_item。 xml

 < RelativeLayout xmlns:android =http://schemas.android.com / apk / res / android
android:layout_width =match_parent
android:layout_height =match_parent
android:padding =40dp
android:background =@ drawable / relative_selector>

< RelativeLayout
android:layout_width =300dp
android:layout_height =100dp
android:background =@ drawable / child_selector_1
android:clickable =true>

< RelativeLayout
android:layout_width =100dp
android:layout_height =100dp
android:layout_centerHorizo​​ntal =true
android: background =@ drawable / child_selector_2
android:clickable =true/>
< / RelativeLayout>
< / RelativeLayout>

relative_selector.xml

 < selector xmlns:android =http://schemas.android.com/apk/res/android> 
< item android:state_pressed =true>
< inset xmlns:android =http://schemas.android.com/apk/res/android>
< shape android:shape =rectangle>
< solid android:color =@ android:color / holo_blue_bright/>
< / shape>
< / inset>
< / item>
< / selector>



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

<! - pressed state - >
< item android:state_pressed =true>
< inset xmlns:android =http://schemas.android.com/apk/res/android>
< shape android:shape =rectangle>
< solid android:color =@ android:color / holo_orange_dark/>
< / shape>
< / inset>
< / item>

<! - focused state - >
< item android:state_focused =true>
< inset xmlns:android =http://schemas.android.com/apk/res/android>
< shape android:shape =rectangle>
< solid android:color =@ android:color / holo_orange_dark/>
< / shape>
< / inset>
< / item>

<! - 正常状态 - >
< item>
< inset xmlns:android =http://schemas.android.com/apk/res/android>
< shape android:shape =rectangle>
< solid android:color =@ android:color / holo_red_dark/>
< / shape>
< / inset>
< / item>
< / selector>


I have a ListView where each list item is a custom layout based on a RelativeLayout, it is clickable and it has a custom selector. En plus, it has a sub-RelativeLayout which is also clickable and it has its own selector.

Something like this:

 ---------------
|               |
|         ___   |
|        |   |  |
 ---------------

Everything works perfectly but the problem is that when I click on the parent relative layout, the child selector assumes the state of the parent selector. I tried to set

android:duplicateParentState="false"

on the child but nothing has changed.

Any idea? Thanks in advance

解决方案

Can you please show your code of ListView's item and selectors? To my mind you've made some mistakes, when implemented it. Here is my example of item and its selectors. It works fine for me:

Video, how my implementation works: >click to open<

list_view_item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="40dp"
    android:background="@drawable/relative_selector">

    <RelativeLayout
        android:layout_width="300dp"
        android:layout_height="100dp"
        android:background="@drawable/child_selector_1"
        android:clickable="true">

        <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_centerHorizontal="true"
            android:background="@drawable/child_selector_2"
            android:clickable="true"/>
    </RelativeLayout>
</RelativeLayout>

relative_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true">
        <inset xmlns:android="http://schemas.android.com/apk/res/android">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/holo_blue_bright" />
            </shape>
        </inset>
    </item>
</selector>

child_selector_1.xml

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

    <!-- pressed state -->
    <item android:state_pressed="true">
        <inset xmlns:android="http://schemas.android.com/apk/res/android">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/holo_orange_dark" />
            </shape>
        </inset>
    </item>

    <!-- focused state -->
    <item android:state_focused="true">
        <inset xmlns:android="http://schemas.android.com/apk/res/android">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/holo_orange_dark" />
            </shape>
        </inset>
    </item>

    <!-- normal state -->
    <item>
        <inset xmlns:android="http://schemas.android.com/apk/res/android">
            <shape android:shape="rectangle">
                <solid android:color="@android:color/holo_red_dark" />
            </shape>
        </inset>
    </item>
</selector>

这篇关于视图从其父级获取其可绘制状态(按下,等...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆