android - 在所有密度的相对布局中定位两个 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>
解决方案
我的问题是,如果我将它拖到我想要的位置,它对于所有密度看起来都不一样
假设您想要的位置从中心稍微向下..
您应该在 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
..
推荐阅读
- scala - 如何在scala中同时完成
- javascript - 如何返回此对象数组的总数?
- python - 使用多个条件查找最大值
- sql-server - 如何将多行中的一列连接起来,然后将该值插入另一行的单列中?
- android - 使用 Uri 加载从内部存储中选择的图像不起作用
- php - 如何使用可变变量 PHP 在 for 循环中创建字符串
- xpages - 浏览器后退按钮 - 中断 onClick 事件
- javascript - 使用 jquery 如何获取父 LI,其中 LI 的文本与我的按钮文本匹配?
- java - Spring AOP 不在代理类的 @Transactional 方法中注入事务
- c# - 使用多个 POST 方法将数据从视图传递到控制器?