在 Android 中的另一个 ImageView 中居中 ImageView [英] Center ImageView inside another ImageView in Android

查看:25
本文介绍了在 Android 中的另一个 ImageView 中居中 ImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我必须将一个 ImageView 放入另一个 ImageView 中.它更小,必须正好在中心.我将两个图像都缩放为不同的屏幕分辨率,但我只能在一部手机上进行测试,我想知道是否使用 dpi 设置第二个图像的高度和宽度以适合我的屏幕分辨率,它会在所有屏幕上完美匹配还是有任何属性在 Android 中,除了设置将自动将一个 ImageView 居中在另一个 ImageView 中的高度和宽度?

Well I have to fit one ImageView inside another one. It is smaller and has to be exactly at the center. I have both images scaled for different screen resolutions but I can test only on one phone and I am wondering if I set the second image's height and width with dpi to fit my screen resolution will it be perfectly matched on all screens or is there any property in Android aside from setting the height and width that will automatically center one ImageView inside of other ImageView?

它在 RelativeLayout 中,ImageViews 在左上角,所以我没有添加任何特定的东西,因为它们默认在那里.

It's in RelativeLayout and the ImageViews are at the top left so I haven't added anything specific to the because they by default are there.

Edit2:将第一个 ImageView 更改为 RelativeLayout 有助于将第二个图像居中,但随后 RelativeLayout 变得太大并放大了作为背景的图像.有没有办法让RelativeLayout 和它的背景一样大?

Well changing the first ImageView to be RelativeLayout helps center the second image inside of it but then the RelativeLayout grows too big and enlarges the image that is background. Is there a way to make the RelativeLayout be as big as its 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"
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>

这篇关于在 Android 中的另一个 ImageView 中居中 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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