【问题标题】:How to align and justify content in center in RelativeLayout?如何在RelativeLayout的中心对齐和对齐内容?
【发布时间】:2019-11-24 03:22:06
【问题描述】:

我尝试将这个 RelativeLayout 的所有内容都对齐并对齐在中心。

MainActivity.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
    android:id="@+id/no_internet"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:visibility="gone"
    android:background="#e8eaf6"
    android:gravity="center"
    >

    <ImageView
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:src="@drawable/no_wifi"
        android:layout_centerInParent="true"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textColor="#404852"
        android:gravity="center"
        android:textSize="25dp"
        android:text="Internet indisponible"
        />

</LinearLayout>

但经过多次尝试,我得到了这个预览: Preview

有人可以帮我证明和对齐图像和居中的文本吗?

【问题讨论】:

标签: android


【解决方案1】:

您应该将LinearLayout 与RelativeLayout 中的属性居中:

android:layout_centerInParent="true"

并将LinearLayout的layout_width和layout_height设置为wrap_content

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/no_internet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:visibility="gone"
        android:background="#e8eaf6"
        android:layout_centerInParent="true"
        android:gravity="center" >

        <ImageView
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:src="@drawable/no_wifi" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#404852"
            android:gravity="center"
            android:textSize="25dp"
            android:text="Internet indisponible" />

    </LinearLayout>
</RelativeLayout>

【讨论】:

  • 谢谢@forpas,这就是我想要的。但即使我将 LinearLayout 的 layout_width 和 layout_height 设置为 match_parent,也可以正常工作。
【解决方案2】:

您已将 LinearLayout 设置为 match_parent。所以LinearLayout里面的子view会按照LinearLayout的规则对齐。

您应该做的是让 LinearLayout 包装其内容并使用 android:layout_centerInParent="true" 将整个 LinearLayout 居中 android:layout_centerHorizo​​ntal="true" android:layout_centerVertical="true"

【讨论】:

    【解决方案3】:

    在相对布局中设置重心,在线性布局中包裹内容的宽度和高度

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-07
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多