【问题标题】:Positioning two ImageViews in a Relative Layout for all densities在所有密度的相对布局中定位两个 ImageView
【发布时间】:2019-02-18 02:31:52
【问题描述】:

所以我有一个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>

【问题讨论】:

  • 如果你想要一个背景图像,将其设置为RelativeLayout,然后使用另一个图像视图作为图像持有者。 “所有密度看起来都不一样”是什么意思
  • 如果我将第二个 ImageView 拖到我想要的位置,hdpi 中的外观与 xxhdpi 中的不同。而且我在单独的可绘制文件夹中有所有密度的不同图像。
  • My problem is if I drag it to place it where I want it to be it doesn't look the same for all densities - 你想把它放在哪里?

标签: android html layout background


【解决方案1】:

我的问题是,如果我将它拖动到我想要的位置,那么它在所有密度下看起来都不一样

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

您应该在 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文件的属性..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-16
    • 1970-01-01
    相关资源
    最近更新 更多