简体   繁体   中英

How to center a vertical LinearLayout horizontally?

I'm not trying to center any View's, since my problem is that I have a dividing line that should go down the middle. To do the dividing line, I made a LinearLayout and gave it a background color. However, there are other LinearLayouts on both sides.

    <LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" 
    android:gravity="center"
    android:layout_gravity="center">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:paddingRight="10dp"
        android:layout_gravity="center">

        <TextView 
            android:id="@+id/changed_text"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/confidential" 
            android:gravity="left"/>

         <TextView 
            android:id="@+id/please_help_me"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="60sp"
            android:text="@string/changed_this_too" 
            android:gravity="center_horizontal"/>
    </LinearLayout>

    <LinearLayout
    android:layout_width="1.5dp"
    android:layout_height="match_parent"
    android:background="#0097ff"
    android:orientation="vertical" 
    android:layout_marginTop="10dp"  
    android:layout_gravity="center" 
    android:gravity="center"/> 

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" 
        android:paddingLeft="10dp"
        android:layout_gravity="center">

        <TextView 
            android:id="@+id/have_tried_everything"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="@string/indeed" 
            android:gravity="left"/>

         <TextView 
            android:id="@+id/you_are_my_last_chance"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:textSize="60sp"
            android:text="@string/yes" 
            android:gravity="center_horizontal"/>
    </LinearLayout>

</LinearLayout>

You can probably tell that the LinearLayout in the middle is the one that makes the line. I have changed a lot of android:layout_width values and also a lot of android:gravity and android:layout_gravity values to try and make it work. But I haven't gotten anything. For some reason my dividing line is always to the left, and never centered. It might have something to do with some of the android:layout_width="wrap_content" values.

I have looked through some guides such as: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

but haven't gotten anything so far.

Help would be seriously appreciated.

尝试将布局宽度更改为0dp,并向第一个和第三个子线性布局添加1.0的布局权重。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM