简体   繁体   中英

Set gravity in LinearLayout - android

I try to use a combination of RelativeLayout and LinearLayout full of TextViews and a button on the bottom. I write a sequence in MainActivity that change the orientation from horizontal to vertical and vice versa. So basically I had to turn lines in columns.

Here is the xml file:

<RelativeLayout 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="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dp"
android:paddingRight="10dp" >

<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingTop="40dp"
    android:paddingRight="16dp"
    android:orientation="horizontal"
    android:gravity="top"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="left"
        android:text="@string/text1"

        />

    <TextView
        android:id="@+id/textv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text2"/>

    <TextView
        android:id="@+id/textv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text3"/>

    <TextView
        android:id="@+id/textv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text4"/>

</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:text="@string/text5"/>

    <TextView
        android:id="@+id/textv6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text6"/>

    <TextView
        android:id="@+id/textv7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text7"/>

    <TextView
        android:id="@+id/textv8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text8"/>


</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="bottom"
    android:foregroundGravity="right"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv9"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="bottom"
        android:text="@string/text9"/>

    <TextView
        android:id="@+id/textv10"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text10" />

    <TextView
        android:id="@+id/textv11"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text11"/>

    <TextView
        android:id="@+id/textv12"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text12"/>


</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

It should appear like this:

应该怎么出现

But it turn like this:

水平的

垂直的

Only the center line stay in the right place the others stack on top of each other.

How does this look? Linear Margin

code:

<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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="40dp">


<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="602dp"
    android:orientation="vertical">


    <LinearLayout
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="147dp"
        android:gravity="top"
        android:orientation="horizontal"
        android:paddingLeft="0dp"
        android:paddingTop="40dp"
        android:paddingRight="16dp"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv1"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="left"
            android:text="@string/text1"

            />

        <TextView
            android:id="@+id/textv2"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text2"


            />

        <TextView
            android:id="@+id/textv3"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text3"

            />

        <TextView
            android:id="@+id/textv4"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text4"

            />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout2"
        android:layout_width="393dp"
        android:layout_height="141dp"
        android:gravity="center"
        android:orientation="horizontal"

        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv5"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="center"
            android:text="@string/text5"

            />

        <TextView
            android:id="@+id/textv6"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text6"


            />

        <TextView
            android:id="@+id/textv7"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text7"

            />

        <TextView
            android:id="@+id/textv8"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text8"

            />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinearLayout3"
        android:layout_width="397dp"
        android:layout_height="104dp"
        android:foregroundGravity="right"
        android:gravity="bottom"
        android:orientation="horizontal"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/textv9"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:layout_gravity="bottom"
            android:text="@string/text9"

            />

        <TextView
            android:id="@+id/textv10"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text10"


            />

        <TextView
            android:id="@+id/textv11"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text11"

            />

        <TextView
            android:id="@+id/textv12"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:text="@string/text12"

            />


    </LinearLayout>
</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>

If you are the textviews to be evenly spaced in a row I suggest you use layout_weight within each LinearLayout, and arrange the LinearLayouts containing the textviews relative to each other. But I really suggest changing your approach. One you set the width and height of textviews to 100dp but Android has many screen sizes so they won't always fit exactly on many devices, and there is a performance hit, using these many views in multiple layout for lower end devices. My suggestion is use a RecyclerView with CardView containing your TextView then use a GridLayout with the recyclerview. You can change the layout of the TextView accordingly via recyclerview inside onClick for button.

Change android:layout_height="match_parent" to android:layout_height="wrap_content" in LinearLayout .

<LinearLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="bottom"
        android:foregroundGravity="right"
        tools:context=".MainActivity">...

I manage to solve the problem by twiking the main file with some lines. Here is the added line:

            if(myll3.getOrientation() == LinearLayout.HORIZONTAL)
                myll3.setGravity(Gravity.BOTTOM | Gravity.CENTER);

            else if(myll3.getOrientation() == LinearLayout.VERTICAL)
                myll3.setGravity(Gravity.RIGHT | Gravity.CENTER);

And I made some changes in the xml file too. Here is the final XML code:

<RelativeLayout 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="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="0dp"
android:paddingRight="0dp" >



<LinearLayout
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="0dp"
    android:paddingTop="100dp"
    android:paddingRight="16dp"
    android:orientation="horizontal"
    android:gravity="top"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text1"

        />

    <TextView
        android:id="@+id/textv2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text2"


        />

    <TextView
        android:id="@+id/textv3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text3"

        />

    <TextView
        android:id="@+id/textv4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text4"

        />



</LinearLayout>
<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingRight="10dp"
    android:gravity="center"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv5"
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:text="@string/text5"

        />

    <TextView
        android:id="@+id/textv6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text6"


        />

    <TextView
        android:id="@+id/textv7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text7"

        />

    <TextView
        android:id="@+id/textv8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text8"

        />


</LinearLayout>

<LinearLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout3"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:paddingBottom="100dp"
    android:paddingTop="100dp"
    android:gravity="bottom"

    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textv9"
        android:layout_width="100dp"
        android:layout_height="100dp"

        android:text="@string/text9"

        />

    <TextView
        android:id="@+id/textv10"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text10"


        />

    <TextView
        android:id="@+id/textv11"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text11"

        />

    <TextView
        android:id="@+id/textv12"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:text="@string/text12"

        />


</LinearLayout>

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button"
    android:layout_alignParentBottom="true"/>

</RelativeLayout>

Thx for your support and your answers.

Here is the final view:- 水平的

垂直的

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