android选择器与imageview可点击的布局 [英] android selector for clickable layout with imageview

查看:101
本文介绍了android选择器与imageview可点击的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请问我是否有这样的布局

guys if i have such layout

<?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

is it any way to write selector that will be change only imageView's img when state is pressed

像这样

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

or show me plz how can i do such button, because the problem is that the right part of button also need background image, and left part is image view that must change it image when pressed on right part (layout)

推荐答案

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

Unless you plan on adding a ton of views to that button layout at runtime...its overly complicated for what you need it to do. Here's a much simpler version that should suite you fine:

<?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,如下所示:

Then in res/drawable you'll want to define btn_background.xml as follows:

<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也会接收到它.这样,选择器drawable便会为您做事并为您更新.

The trick here is that the LinearLayout is made clickable. That way the user can click anywhere on the 'button'. Then have the ImageView duplicate it's parent state. Which means whenever the LinearLayout changes it's state (Eg, enabled, focused, pressed, etc), the ImageView will also receive it. Whereby the selector drawable will do its thing and update for you.

这篇关于android选择器与imageview可点击的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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