【问题标题】:Android how to allign these viewsAndroid如何对齐这些视图
【发布时间】:2016-01-19 08:35:56
【问题描述】:

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

我必须在顶部的“弹出”中间对齐文本。

现在我的布局如下:

<?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 是硬编码的,它不会在所有设备上提供所需的结果。
我想为这个“弹出窗口”使用 9patch,但将它指向地图上的确切位置仍然是个问题。

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

【问题讨论】:

    标签: android android-relativelayout


    【解决方案1】:

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

    只需遵循指南:

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

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 2021-07-31
      • 2014-02-17
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多