Android 如何对齐这些视图

Android how to allign these views

我正在尝试在 android 布局中对齐两个视图。
我有一个 ImageView

而且我必须在顶部 "popup" 的中间对齐文本。

现在我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/rl_authorization_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="top"
    android:padding="35dp"
    >

    <ImageView
        android:id="@+id/iv_map"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="50dp"
        android:adjustViewBounds="true"
        android:src="@drawable/map_contact"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:layout_alignTop="@+id/iv_map"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:gravity="center"
        android:text="@string/address"
        android:textColor="@color/black"
        android:textSize="16sp"/>
</RelativeLayout>

但是如你所见, android:layout_marginTop="20dp" TextView 的部分是硬编码的,它不会在所有设备上都提供所需的结果。
我想为此 "popup" 使用 9patch,但将其指向地图上的确切位置仍然是个问题。

我是否应该以某种方式计算实际的 ImageView 高度并将 layout_marginTop 设置为实际高度的百分比?

如果弹出图像针对所有分辨率正确缩放,您可以写 android:layout_marginTop="20dp"。

只需遵循指南:

  • 小:1 dp = 0.5 像素
  • 中:1 dp = 1 像素
  • 大:1 dp = 1.5 像素
  • 超大:1 dp = 2 像素
  • xxlarge:1 dp = 3 像素
  • xxxlarge: 1 dp = 4 像素