Home > Net >  Linear layout split half to screen
Linear layout split half to screen

Time:07-05

I am using the adapter to show data to the screen and I am using a grid layout.

The single grid consists of a linear layout with two text views one to show the heading and one to show the body. How can I split that to use half-width of the screen and wrap the content to height. So can both can use equal screen.

Actually, I don't want to split both text views half and half, I want two different grids to consume half and half screen. You can see here what I want to say i want to center gridview

Here is My XML


    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="100dp"
    android:layout_marginTop="70dp"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <TextView
        android:id="@ id/text_title_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="@color/white"
        android:gravity="start"
        android:textColor="@color/black"
        android:fontFamily="@font/futuraheavyfont"
        android:textSize="26sp"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        android:text="Title" />

    <TextView
        android:id="@ id/text_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:fontFamily="@font/futuramediumbt"
        android:textSize="20sp"
        android:gravity="start"
        android:background="@color/white"
        android:theme="@style/ThemeOverlay.AppCompat.Light"
        android:text="Body will go here" />
</LinearLayout>




CodePudding user response:

Try This

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="2"
    >
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
</LinearLayout>

CodePudding user response:

Try this

<?xml version="1.0" encoding="utf-8"?>
<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="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:layout_weight="1"
        android:columnCount="2"
        android:rowCount="2">
        
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#00BCD4"
            android:gravity="center"
            android:orientation="vertical"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_column="0"
            android:layout_columnWeight="1"
            android:layout_gravity="fill"
            android:padding="20dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="TEXT"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:text="TEXT1"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="16sp"
                android:textStyle="bold" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="#FF5722"
            android:gravity="center"
            android:orientation="vertical"
            android:padding="20dp"
            android:layout_row="0"
            android:layout_rowWeight="1"
            android:layout_column="1"
            android:layout_columnWeight="1"
            android:layout_gravity="fill">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="TEXT2"
                android:paddingTop="10sp"
                android:paddingBottom="10sp"
                android:textAlignment="center"
                android:textColor="@color/black"
                android:textSize="16sp"
                android:textStyle="bold" />

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:textAlignment="center"
                android:padding="10dp"
                android:textSize="16sp"
                android:textColor="@color/white"
                android:textStyle="bold"
                android:text="TEXT3" />


        </LinearLayout>
        
    </GridLayout>

</LinearLayout>
  • Related