我尝试在中心对齐并证明此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
有人可以帮我对图像和文本居中对齐吗?
答案 0 :(得分:0)
您已将LinearLayout设置为match_parent。因此,LinearLayout中的子视图将根据LinearLayout的规则对齐。
您应该做的是使用android:layout_centerInParent =“ true”,让LinearLayout包装其内容并居中放置整个LinearLayout android:layout_centerHorizontal =“ true” android:layout_centerVertical =“ true”
答案 1 :(得分:0)
您应将LinearLayout
放在RelativeLayout
内部,并使用以下属性:
android:layout_centerInParent="true"
并将layout_width
的{{1}}和layout_height
设置为LinearLayout
wrap_content
答案 2 :(得分:0)
只需在相对布局中设置重心,并在线性布局中包装内容的宽度和高度