如何在xml中居中整个线性布局?

时间:2021-10-05 19:11:41

I have a few vertically oriented elements in a LinearLayout

我在线性布局中有一些垂直方向的元素

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:orientation="vertical" >

    <TextView
            ... />

    <Button
            ... />
</LinearLayout>

These elements do not take up the entire vertical space of the application, and I want them to be vertically centered. How do I do this?

这些元素不会占用应用程序的整个垂直空间,我希望它们是垂直居中的。我该怎么做呢?

3 个解决方案

#1


1  

the attribute android:layout_gravity positions the view with respect to its parent, meaning their enclosing layout. In your case, the problem is, that you have no enclosing layout. I know, it's confusing because one might think that the LinearLayout would center itsself in the root window, but even when it comes to attributes which basically refer to their parent e.g "android:layout_centerInParent" they always refer to their enclosing layout ("parent"). By the way, "LinearLayout" doesn't support those attributes.

属性android:layout_gravity对其父视图进行定位,意思是它们的封闭布局。在您的例子中,问题是,您没有封闭布局。我知道,这很让人困惑,因为有人可能会认为线性布局会在根窗口中把它的自我中心,但即使是在属性上,也就是他们的父母e。g“android:layout_centerInParent”他们总是指他们所包含的布局(“parent”)。顺便说一下,“LinearLayout”不支持这些属性。

My ideas to solve your problem:

我解决你问题的想法:

Solution 1: put your LinearLayout in an enclosing layout:

解决方案1:将线性布局放在封闭布局中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Textbox1"  />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Textbox2"  />     

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

    </LinearLayout>

</FrameLayout>

Solution 2: let the containing views align themselves

解决方案2:让包含的视图自己对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textview_1"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Textbox1" />

    <TextView
        android:id="@+id/textview_2"
        android:layout_below="@id/textview_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Textbox2" />      

    <Button
        android:layout_below="@id/textview_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

</RelativeLayout>

Hope that helps!

希望会有帮助!

#2


2  

Layout_gravity="center_vertical" does not work on a vertical LinearLayout.

Layout_gravity="center_vertical"在垂直线性布局上不起作用。

I'd say you can set android:layout_height="match_parent" and android:gravity="center_vertical" on the LinearLayout.

我想说你可以在线性布局上设置android:layout_height="match_parent"和android:gravity="center_vertical"。

#3


2  

Try out this way :

试试这个方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >    
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="lhkabhfksbj" />    
</LinearLayout>

Just set the gravity of views to "center" . I think this is what you want.

只需将视图的重心设置为“center”即可。我想这就是你想要的。

#1


1  

the attribute android:layout_gravity positions the view with respect to its parent, meaning their enclosing layout. In your case, the problem is, that you have no enclosing layout. I know, it's confusing because one might think that the LinearLayout would center itsself in the root window, but even when it comes to attributes which basically refer to their parent e.g "android:layout_centerInParent" they always refer to their enclosing layout ("parent"). By the way, "LinearLayout" doesn't support those attributes.

属性android:layout_gravity对其父视图进行定位,意思是它们的封闭布局。在您的例子中,问题是,您没有封闭布局。我知道,这很让人困惑,因为有人可能会认为线性布局会在根窗口中把它的自我中心,但即使是在属性上,也就是他们的父母e。g“android:layout_centerInParent”他们总是指他们所包含的布局(“parent”)。顺便说一下,“LinearLayout”不支持这些属性。

My ideas to solve your problem:

我解决你问题的想法:

Solution 1: put your LinearLayout in an enclosing layout:

解决方案1:将线性布局放在封闭布局中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

     <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:orientation="vertical" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Textbox1"  />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Textbox2"  />     

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button1" />

    </LinearLayout>

</FrameLayout>

Solution 2: let the containing views align themselves

解决方案2:让包含的视图自己对齐

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textview_1"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Textbox1" />

    <TextView
        android:id="@+id/textview_2"
        android:layout_below="@id/textview_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:text="Textbox2" />      

    <Button
        android:layout_below="@id/textview_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button1" />

</RelativeLayout>

Hope that helps!

希望会有帮助!

#2


2  

Layout_gravity="center_vertical" does not work on a vertical LinearLayout.

Layout_gravity="center_vertical"在垂直线性布局上不起作用。

I'd say you can set android:layout_height="match_parent" and android:gravity="center_vertical" on the LinearLayout.

我想说你可以在线性布局上设置android:layout_height="match_parent"和android:gravity="center_vertical"。

#3


2  

Try out this way :

试试这个方法:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:orientation="vertical" >    
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="lhkabhfksbj" />    
</LinearLayout>

Just set the gravity of views to "center" . I think this is what you want.

只需将视图的重心设置为“center”即可。我想这就是你想要的。