将两个ImageView放置在相对布局中以适用于所有密度

问题描述 投票:0回答:1

所以我有一个RelativeLayout,我放了两个ImageViews,一个作为背景图像,另一个作为背景图像位于中间,但稍微朝向底部(见下图)。我有所有6种密度的图像。我的问题是,如果我拖动它将它放在我希望它的位置,它对于所有密度看起来都不一样。 enter image description here

<?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>
android html layout background
1个回答
0
投票

我的问题是,如果我拖动它将它放在我希望它的位置,它对于所有密度看起来都不一样

假设您想要的位置略微偏离中心位置..

你应该在drawables文件夹下创建一个XML ...比如说.. shape.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文件的属性..

© www.soinside.com 2019 - 2024. All rights reserved.