我有一个启动画面的 Activity layout.xml,它基本上显示了我想要的内容。但是如果我在更大或更小的屏幕设备上打开应用程序,它就不再居中了。
有没有办法在元素周围放置一个包装器并将其在屏幕的高度和宽度上居中?还有其他选择吗?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:background="@color/colorPrimaryDark" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="250dp"
android:layout_marginBottom="7dp"
android:text="@string/head_line"
android:textColor="@color/white"
android:textSize="50sp"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="@mipmap/ic_launcher_round"
android:layout_gravity="center" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="7dp"
android:text="@string/slogan"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
现在,它看起来像这样:
提前谢谢大家!
最佳答案
试试这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:background="@color/colorPrimaryDark"
android:gravity="center_vertical|center_horizontal"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="7dp"
android:text="@string/head_line"
android:textColor="@color/white"
android:textSize="50sp"
android:textStyle="bold" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:id="@+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="7dp"
android:text="@string/slogan"
android:textColor="@color/white"
android:textSize="24sp" />
</LinearLayout>
关于安卓布局 : Center Text and Image View to screen center?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47579492/