将图像设置为imageview的最佳方法是什么

时间:2011-06-28 12:17:06

标签: android android-layout

您好我已经创建了一个相对布局。并将imageview对齐relativelayout右侧。我使用选择器设置了relativelayout的背景。如果我选择相对布局,其颜色将为绿色,否则其颜色将为白色。现在我想根据scrollview的选择设置图像。如果选择了scrollview,那么我想显示imge1,如果没有,那么我想显示image2。我怎样才能做到这一点?

main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/selector">
    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_right"

        android:layout_alignParentRight="true" />
</RelativeLayout>

selector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/grbk" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/grbk" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/grbk" />
    <item
        android:drawable="@drawable/whbk" />
</selector>

由于 苏尼enter image description here

4 个答案:

答案 0 :(得分:1)

与背景相同。你应该添加 android:duplicateParentState =“true”

<ImageView
        android:duplicateParentState="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/arrow_selector"
        android:layout_alignParentRight="true" />

答案 1 :(得分:0)

首先将颜色设置为所有列表列表视图项,然后将选择器放在列表视图中。我知道如何在列表视图中设置选择器

答案 2 :(得分:0)

我们可以为imageview创建另一个选择器,并将该选择器作为背景传递给imageview。

<强> imageselector.xml

<selector
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_focused="true"
        android:state_pressed="false"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="true"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:state_focused="false"
        android:state_pressed="true"
        android:drawable="@drawable/arow_selected" />
    <item
        android:drawable="@drawable/arow_unselect" />
</selector>

<强> main.xml中     

        <ImageView
        android:id="@+id/arrow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/imageselector"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="20dip"/>
</RelativeLayout>

答案 3 :(得分:0)

这是我的建议.. 一旦我将背景设置为imageview,那么当我想要更改透明时,我什么都不做...在检查一些网站后,我发现我需要设置imageview的资源而不是背景......