用于imageview的可点击布局的android选择器

iCa*_*sar 4 layout android clickable

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

<?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>
Run Code Online (Sandbox Code Playgroud)

结果是

结果

是否有任何方式来编写选择器,只有在按下状态时才会更改imageView的img

像这样

压制

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

Jay*_*yer 12

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

<?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>
Run Code Online (Sandbox Code Playgroud)

然后在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>
Run Code Online (Sandbox Code Playgroud)

这里的诀窍是LinearLayout可以点击.这样,用户可以点击"按钮"上的任意位置.然后ImageView重复它的父状态.这意味着每当LinearLayout改变它的状态(例如,启用,聚焦,按下等)时,ImageView也会接收它.因此,选择器drawable将做它的事情并为您更新.