在另一个android上面设置一个图像

时间:2013-10-29 12:23:11

标签: java android xml

我想创建一个这样的布局:

enter image description here

我有3个不同的图像:背景,白色方块和V形符号。

当我希望盒子从一侧移动到另一侧时,如何在照片中定位它们

触发onClick时。

我试过了:

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

    <ImageView
    android:id="@+id/bottom_layer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/handle" />

<ImageView
    android:id="@+id/upper_layer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/switch_v" />

</FrameLayout>

正确地给了我订单,但没有正确提出:

enter image description here

4 个答案:

答案 0 :(得分:1)

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

    <ImageView android:id="@+id/checkButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/handle"
        android:src="@drawable/switch_v"
        android:layout_gravity="center"
        android:scaleType="centerInside"/>

</FrameLayout>

答案 1 :(得分:0)

尝试一下:

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/off_background" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" >

                <ImageView
                    android:id="@+id/bottom_layer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/handle" />

                <ImageView
                    android:id="@+id/upper_layer"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:src="@drawable/switch_v" />
            </RelativeLayout>
        </RelativeLayout>

答案 2 :(得分:0)

在xml中为ImageView提供android:layout_gravity="center"

答案 3 :(得分:0)

你应该真正关注TobbleButton

您的布局应如下所示

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/off_background">
    <ToggleButton 
        android:id="@+id/toggle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn=""
        android:textOff=""
        android:background="@drawable/toggle_bg"
    />

</FrameLayout>

使用选择器(toggle_bg)作为背景,如下所示

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

这当然假设你有ToggleButton不同状态的drawable。

希望有所帮助。

相关问题