【问题标题】:View gets its drawable state (pressed, etc...) from its parentView 从其父级获取其可绘制状态(按下等)
【发布时间】:2011-12-05 03:27:18
【问题描述】:

我有一个 ListView,其中每个列表项都是基于 RelativeLayout 的自定义布局,它是可点击的,并且有一个自定义选择器。另外,它有一个子RelativeLayout,它也是可点击的,它有自己的选择器。

类似这样的:

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

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

android:duplicateParentState="false"

在孩子身上,但没有任何改变。

有什么想法吗?提前致谢

【问题讨论】:

    标签: android android-layout css-selectors


    【解决方案1】:

    能否请您显示您的 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_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>
    

    【讨论】:

      猜你喜欢
      • 2012-01-31
      • 1970-01-01
      • 1970-01-01
      • 2015-06-26
      • 2011-02-15
      • 1970-01-01
      • 2021-12-04
      • 2013-10-02
      • 2016-09-14
      相关资源
      最近更新 更多