在Android中,如何将ImageButton与背景图像正确对齐?

在Android中,如何将ImageButton与背景图像正确对齐?,android,alignment,android-imagebutton,Android,Alignment,Android Imagebutton,在使用XML的Android UI设计中,如何才能将ImageButton与活动XML文件的背景精确对齐 假设我有两个图像,一个作为活动的背景图像,另一个作为图像按钮源 这是背景图像。 这是按钮图像 我将设置第一个图像作为活动的背景。我的问题是如何正确地放置和对齐第二个图像,即位于背景“中心矩形”内的按钮。“中心矩形”是占位符,它可以位于屏幕中的任何位置 不是:我尝试使用相对布局,但是,无法根据背景实际放置按钮 编辑:- 实际上,背景中心的矩形和圆角矩形只是一个占位符。它可以是任何东西,甚至什

在使用XML的Android UI设计中,如何才能将ImageButton与活动XML文件的背景精确对齐

假设我有两个图像,一个作为活动的背景图像,另一个作为图像按钮源

这是背景图像。

这是按钮图像

我将设置第一个图像作为活动的背景。我的问题是如何正确地放置和对齐第二个图像,即位于背景“中心矩形”内的按钮。“中心矩形”是占位符,它可以位于屏幕中的任何位置

不是:我尝试使用相对布局,但是,无法根据背景实际放置按钮

编辑:-
实际上,背景中心的矩形和圆角矩形只是一个占位符。它可以是任何东西,甚至什么都不是。或者它可以在任何地方。它可能不在中心。考虑整个图像,我需要把按钮图像放在位置保持器上。这是我的意图。比如说,考虑一个无线电应用,那里有一个作为音量摇杆的旋转按钮。图像中的所有其他内容都是背景,音量摇杆可能是另一个图像。

答案是,您尝试的方式是不可能的

你要做的是进一步切割背景,使按钮居中

以图像为例,下面是视图层次结构的外观

->FrameLayout1
---->Framelayout2
-------->Button
FrameLayout1将是没有内部正方形的背景

FrameLayout2将是内部正方形

按钮将显示按钮资源,并放置在FrameLayout 2中的中心位置


除此之外,您还需要使用其他技术使其看起来像像素一样完美,例如使用9块补丁绘图板。

考虑到您正在以横向模式进行尝试(您的图像似乎如此) 您可以尝试以下代码

背景:你的背景

你的中心形象

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/img" />

</RelativeLayout>


可绘制/img_bg.xml
或
bg.png和fr.png是高度和宽度相同的透明图像。

首先将这些图像设置为透明图像。然后尝试使用图层列表并检查。@HarshaVardhan你能给图层列表的概念一些启发吗?我已经发布了答案。谢谢你的回复。实际上,背景中心的矩形和圆角矩形只是一个占位符。它可以是任何东西,甚至什么都不是。考虑整个图像,我需要把按钮图像放在位置保持器上。这是我的意图。比如说,考虑一个无线电应用,那里有一个作为音量摇杆的旋转按钮。图像中的所有其他内容都是背景,音量摇杆可能是不同的图像。我实际上有一个想法,将按钮放在中间,但背景中显示的占位符可以是任何位置,可能是顶部、中间、底部、任何位置。请看我问题中的编辑。
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

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

    </RelativeLayout>

drawable/img_bg.xml
    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

        <item android:drawable="@drawable/bg">
        </item>
        <item android:drawable="@drawable/fr">
        </item>

    </layer-list>

or

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:src="@drawable/bg" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignTop="@+id/imageView1"
            android:src="@drawable/fr" />

    </RelativeLayout>