【问题标题】:Android: Relative Layout Selector Doesn't WorkAndroid:相对布局选择器不起作用
【发布时间】:2014-04-18 10:35:17
【问题描述】:

我正在使用带有选择器资源 xml 的相对布局来突出显示点击时的相对布局。但它不会突出显示点击时的相对布局。下面我放了相应的选择器xml、布局xml和drawable。

布局代码片段:

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="2"
                android:id="@+id/rlShare"
                android:background="@drawable/icon_selector"
                android:clickable="true">

                <ImageView
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_centerInParent="true"
                    android:contentDescription="@string/cd_new_exam"
                    android:scaleType="fitXY"
                    android:src="@drawable/share_icon" />
            </RelativeLayout>

选择器 xml 文件:

<!-- Selected -->
<item android:drawable="@drawable/icon_selected" android:state_focused="true" android:state_selected="false"/>

<!-- Pressed -->
<item android:drawable="@drawable/icon_selected" android:state_focused="false" android:state_selected="true"/>

请帮帮我,

提前谢谢你。

【问题讨论】:

  • 还在选择器中添加android:state_pressed="true" 之类的状态并尝试
  • 您可以在选中或按下时拍摄相同的图像

标签: android android-layout android-relativelayout android-selector


【解决方案1】:

使用此代码并从可绘制的图像中获取不同的图像

 <!-- Selected -->
<item android:drawable="@drawable/icon_selected" android:state_focused="true"         android:state_selected="true"/>

<!-- Pressed -->
<item android:drawable="@drawable/icon_pressed" android:state_focused="false" android:state_selected="true"/>

<!-- When not selected -->
<item android:drawable="@drawable/icon_default"/>

【讨论】:

    【解决方案2】:

    将此用于您的选择器:

    <!-- Selected -->
    <item android:drawable="@drawable/icon_selected" android:state_selected="true"/>
    
    <!-- Pressed -->
    <item android:drawable="@drawable/icon_pressed" android:state_pressed="true"/>
    
    <!-- Default -->
    <item android:drawable="@drawable/icon_default" />
    

    请注意,在您的原始选择器中,您在两种状态下都有相同的可绘制对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-19
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-20
      相关资源
      最近更新 更多