簡體   English   中英

如何在RelativeLayout的中心對齊和對齊內容?

[英]How to align and justify content in center in RelativeLayout?

我嘗試在中心對齊並證明此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>

但經過多次嘗試,我得到了以下預覽: 預覽

有人可以幫我對圖像和文字居中對齊嗎?

您已將LinearLayout設置為match_parent。 因此,LinearLayout中的子視圖將根據LinearLayout的規則對齊。

您應該做的是使用android:layout_centerInParent =“ true” android:layout_centerHorizo​​ntal =“ true”允許LinearLayout包裝其內容並將整個LinearLayout居中

您應該使用以下屬性將LinearLayout放在RelativeLayout內部:

android:layout_centerInParent="true"

並將LinearLayoutlayout_widthlayout_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>

只需在相對布局中設置重心,並在線性布局中包裝內容的寬度和高度

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM