如何在RelativeLayout的中心对齐和对齐内容?

时间:2019-11-23 18:06:43

标签: android

我尝试在中心对齐并证明此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

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

3 个答案:

答案 0 :(得分:0)

您已将LinearLayout设置为match_parent。因此,LinearLayout中的子视图将根据LinearLayout的规则对齐。

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

答案 1 :(得分:0)

您应将LinearLayout放在RelativeLayout内部,并使用以下属性:

android:layout_centerInParent="true"

并将layout_width的{​​{1}}和layout_height设置为LinearLayout

wrap_content

答案 2 :(得分:0)

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

相关问题