Java 将一个图像置于另一个图像之上

Java 将一个图像置于另一个图像之上,java,android,xml,Java,Android,Xml,我想创建如下布局: 我有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" a

我想创建如下布局:

我有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>

这给了我正确的顺序,但姿势不正确:

试试看:

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

为xml中的ImageView提供android:layout\u gravity=“center”


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

你真的应该看看

您的布局应该如下所示

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

当然,这是假设您具有用于切换按钮不同状态的可绘制项


希望这能有所帮助。

谢谢,但我必须使用淡入淡出动画来切换v开关,以及从color1到Color2C的bg颜色转换。我可以用RelativeLayout来完成吗?当然可以。只需使用RelativeLayout更改框架布局文本