在Android中将ImageView居中放置到另一个ImageView中

6
我需要在一个ImageView中放置另一个ImageView,并且它比较小,必须完全位于中心。我已经将两个图像缩放用于不同的屏幕分辨率,但我只能在一部手机上测试,我想知道如果我使用dpi设置第二个图像的高度和宽度以适应我的屏幕分辨率,是否会在所有屏幕上完美匹配,或者除了设置高度和宽度之外,在Android中是否有任何属性可以自动将一个ImageView居中放置到另一个ImageView中?
编辑:它在RelativeLayout中,并且ImageView位于左上角,因此我没有添加任何特定内容,因为它们默认存在。
编辑2:将第一个ImageView更改为RelativeLayout有助于使第二个图像居中放置在其中,但是RelativeLayout会变得太大并且扩大了背景图片。有没有办法让RelativeLayout的大小与其背景一样大?
4个回答

11

试一试

<?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"
android:orientation="vertical" >

  <RelativeLayout
   android:layout_width="250dp"
   android:layout_height="250dp"  >

   <ImageView 
       android:id="@+id/base"
       android:layout_height="250dp"
       android:layout_width="250dp"
       android:src="@drawable/home"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true" />

    <ImageView 
       android:id="@+id/center"
       android:layout_height="150dp"
       android:layout_width="150dp"   
       android:src="@drawable/home"
       android:layout_centerInParent="true"
      android:layout_centerVertical="true"   />
 </RelativeLayout>

</RelativeLayout>

好的,这个方法可以用,但是我需要RelativeLayout与我在所有屏幕上设置为背景的图片一样大,而将宽度和高度设置为wrap_content不起作用。 - Sasho
无帮助。编辑:它可以工作,我使用了错误的图像。 - Sasho

3
您不能将一个imageView放置在另一个imageView内部。希望以下代码对您有所帮助。
<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_image"
        android:gravity="center" >

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

    </LinearLayout>

0

试试这个:

android:layout_gravity="center"

0
使用RelativeLayout,然后使用android:layout_centerInParent="true",记住您添加ImageViews的顺序将决定哪个在顶部 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接