【问题标题】:Why gravity doesn't center all of my views?为什么重力不能集中我所有的观点?
【发布时间】:2017-02-01 20:05:54
【问题描述】:

我希望我的所有 TextView 在我的 RelativeLayout 中垂直居中,但这只是徽标之一的情况。其他的则固定在后者的顶部。

Image

这是我的布局:

<?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="72dp"
android:gravity="center_vertical">

<TextView
    android:id="@+id/logo"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:text="TextView" />

<TextView
    android:id="@+id/stop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/logo"
    android:layout_toRightOf="@+id/logo"
    android:text="TextView" />

<TextView
    android:id="@+id/eta"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toEndOf="@+id/stop"
    android:layout_toRightOf="@+id/stop"
    android:text="TextView" />

<TextView
    android:id="@+id/direction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/stop"
    android:layout_toEndOf="@+id/logo"
    android:layout_toRightOf="@+id/logo"
    android:text="TextView" />

【问题讨论】:

    标签: android android-layout layout-gravity


    【解决方案1】:

    属性layout_toEndOf进行布局如下:

    将此视图的起始边缘定位到给定锚视图 ID 的末尾。 Link

    这意味着您的所有TextViews 开始边缘与logo 的结束对齐。 作为起点,只需删除所有 layout_toEndOf 属性。

    那么你的布局应该是这样的:

    +--------------+
    |              | +-----------+
    |              | |   stop    |
    |              | +-----------+ +-----------+
    |     logo     |/* no space */ |    eta    |
    |              | +-----------+ +-----------+
    |              | | direction |
    |              | +-----------+
    +--------------+
    

    更准确地说,logo 垂直居中,与eta 相同。由于您的布局代码明确地将stopdirection 直接放在logo 的右侧,因此directionstop 下方。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 2013-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多