【问题标题】:android selector for clickable layout with imageview带有imageview的可点击布局的android选择器
【发布时间】:2014-02-14 16:09:29
【问题描述】:

伙计们,如果我有这样的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/deals_list_item_bckg"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_margin="15dp"
        android:background="@drawable/deals_list_item_background"
        android:clickable="true"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="85dp"
            android:layout_height="50dp"
            android:scaleType="fitXY"
            android:src="@drawable/btn_unpressed" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center_vertical|right"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingRight="10dp"
                android:text="My Account"
                android:textSize="16sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

结果是

有没有办法编写选择器,当按下状态时只改变 imageView 的 img

喜欢这个

或者请告诉我我该怎么做这样的按钮,因为问题是按钮的右侧部分也需要背景图像,而左侧部分是图像视图,当按下右侧部分(布局)时必须更改它的图像

【问题讨论】:

  • 给ImageView的imageDrawable设置一个StateListDrawable

标签: android layout clickable


【解决方案1】:

除非您计划在运行时向该按钮布局添加大量视图......它对于您需要它执行的操作来说过于复杂。这是一个更简单的版本,应该适合您:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_margin="15dp"
android:clickable="true"
android:orientation="horizontal">

<ImageView
    android:layout_width="85dp"
    android:layout_height="match_parent"
    android:scaleType="fitXY"
    android:duplicateParentState="true"
    android:src="@drawable/btn_background" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|right"
    android:background="@drawable/your_grey_bar"
    android:paddingRight="10dp"
    android:text="My Account"
    android:textSize="16sp" />
</LinearLayout>

然后在 res/drawable 中你需要定义 btn_background.xml 如下:

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

<item android:drawable="@drawable/btn_default_disabled_focused" android:state_focused="true" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_default_focused" android:state_focused="true" android:state_enabled="true"/>
<item android:drawable="@drawable/btn_default_disabled" android:state_enabled="false"/>
<item android:drawable="@drawable/btn_default_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/btn_default_normal" />

</selector>

这里的诀窍是使 LinearLayout 可点击。这样,用户可以单击“按钮”上的任何位置。然后让ImageView 复制它的父状态。这意味着每当 LinearLayout 更改其状态(例如,启用、聚焦、按下等)时,ImageView 也会收到它。因此,选择器可绘制对象将为您完成它的工作并更新。

【讨论】:

  • 工作得很好,但在设备上我的按钮是全屏的 =( 我该如何解决它
  • 只需用另一个适当的布局围绕 LinearLayout。例如,线性、框架、相对等。取决于您是否真的添加了其他东西。它的高度/宽度需要为 match_parent
【解决方案2】:

使用Android提供的样式如下

<ImageView
    android:src="@drawable/sky_image"
    style="@style/Widget.AppCompat.ActionButton"
    ...
/>

你会在点击时获得色调效果

source

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多