如何将所有视图嵌套到一个视图中,该视图垂直居中放置



这是我的主活动.xml我正在使用LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffe6cc"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zorgan.app.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="#333"
        android:textSize="30sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteY="116dp" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:srcCompat="@drawable/front"
        android:contentDescription="@string/front_desc" />
    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        tools:layout_editor_absoluteY="496dp" />
</LinearLayout>

如何将这 3 个视图嵌套到一个视图中,以便它们都垂直位于中心位置?

若要垂直居中子视图,请为父视图提供android:gravity="center_vertical"。在您的情况下,对于线性布局。

在这里试试这个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="#ffe6cc"
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zorgan.app.MainActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/app_name"
        android:textColor="#333"
        android:textSize="30sp"
        android:textStyle="bold"
        tools:layout_editor_absoluteY="116dp" />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        app:srcCompat="@drawable/front"
        android:contentDescription="@string/front_desc" />
    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:layout_editor_absoluteY="496dp" />
</LinearLayout>

在此android:gravity="center"中是关键参数。

将代码替换为,

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffe6cc"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:id="@+id/textView"
    android:layout_gravity="center_vertical|center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/app_name"
    android:textColor="#333"
    android:textSize="30sp"
    android:textStyle="bold"
    tools:layout_editor_absoluteY="116dp" />
<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:layout_gravity="center_vertical|center_horizontal"
    app:srcCompat="@drawable/front"
    android:contentDescription="front_desc" />
<Button
    android:id="@+id/login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical|center_horizontal" />
 </LinearLayout>

它经过测试并正常工作。

最新更新