Android 如何将所有视图嵌套到一个垂直居中的视图中?

Android 如何将所有视图嵌套到一个垂直居中的视图中?,android,android-layout,android-studio,Android,Android Layout,Android Studio,这是我的MainActivity.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.andr

这是我的MainActivity.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>


如何将这三个视图嵌套到一个视图中,使它们都垂直居中?

若要使子视图垂直居中,请为父视图指定
android:gravity=“center\u 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>


它经过测试并正常工作。

您需要什么?您需要将内部三个视图居中?是的。目前,他们都坐在屏幕的顶部(相互重叠),我希望他们在屏幕的中央彼此重叠。不清楚你在问什么。请具体说明您的问题,如果可能,请添加屏幕截图。设置android:gravity in LinearLayout只需在LinearLayour中使用高度包裹内容,在RelativeLayout中使用高度包裹内容,或使用gravity center_垂直框架布局。快乐编码。干杯:)抄袭者:P这和@V-rund的答案一样。@Shadow希望你不是瞎子。。吃些饼干。