如何在Android中将ImageButton与背景图像正确对齐? [英] How to place ImageButton properly aligned to the background image in Android?

查看:95
本文介绍了如何在Android中将ImageButton与背景图像正确对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用XML的Android UI设计中,如何放置一个ImageButton使其与活动xml文件的背景完全对齐。

In Android UI design using XML, how is it possible to put an ImageButton to align exactly with the background of the activity xml file.

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

Suppose, I have two images, one acts as the background image for the activity, and the second one acts as the image button source.

这是背景图像。
http://i.stack.imgur.com/e1Ow5.png

这是按钮图像。 http://i.stack.imgur.com/m0tUU.png

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

I will set the first image as background to the activity. My question is how will I properly place and align the second image,that is the button to be exactly inside the "central rectangle" of the background. The "central rectangle" is the place holder, and it can be anywhere in the screen.

不是:我尝试使用相对布局,但是,实际上无法放置

Not: I tried using relative layout, but, couldn't really place the button depending on the background.

编辑:-
实际上,矩形和位于背景中心的圆角矩形只是一个位置-持有人。它可以是任何东西,甚至什么也没有。或者它可以在任何地方。它可能不在中心。考虑整个图像,我需要将按钮图像放在占位符所在的位置。那是我的意图。例如,假设广播应用程序中有一个充当音量摇杆的旋转按钮。图像中的其他所有内容都是背景,音量摇杆可能是不同的图像。

推荐答案

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

bg.png和fr.png是具有相同高度和宽度的透明图像。


bg.png and fr.png are transparent image with same height and width.

这篇关于如何在Android中将ImageButton与背景图像正确对齐?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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