首页 > 解决方案 > 在所有密度的相对布局中定位两个 ImageView

问题描述

所以我有一个RelativeLayout,我在其中放置了两个ImageView,一个作为背景图像,另一个在中间,但有点靠近底部(见下图)。我有所有 6 种密度的图像。我的问题是,如果我将它拖到我想要的位置,它对于所有密度来说看起来都不一样。在此处输入图像描述

<?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:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:scaleType="centerCrop"
    android:src="@drawable/background" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="87dp"
    android:scaleType="centerCrop"
    android:src="@drawable/img2" />
</RelativeLayout>

标签: androidhtmllayoutbackground

解决方案


我的问题是,如果我将它拖到我想要的位置,它对于所有密度看起来都不一样

假设您想要的位置从中心稍微向下..

您应该在 drawables 文件夹下创建 am XML.. 比如说.. 例如。形状.xml

下面是它的示例代码..

res >drawables >shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
android:shape="rectangle">
<solid android:color="@android:color/transparent"/>
<corners android:radius="12px"/> 
<stroke android:width="2dip" android:color="#000000"/>  
</shape>

现在在您的 Activity XML 中添加一个属性ImageView并设置形状的背景和大小,如下所示:

<ImageView 
android:id="@+id/rect_image" 
android:layout_height="150dp"   // change as per your requirements
android:layout_width="250dp"   // change as per your requirements
android:background="@drawable/shape"    // important one
android:paddingTop="75dp"   // position the shape as per your requirements here
android:src="@drawable/shape">
</ImageView>

您现在将在所有屏幕密度中获得文件中设计的形状shape.xml..

PS:根据您的要求更改文件的属性shape.xml..


推荐阅读