Tuesday, June 21, 2016

android - Understanding LinearLayout weight and weightSum

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8d938b"
    android:weightSum="3"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LinearLayout Weight Sum = 3\nTextView 1, Layout weight = 1"
        android:layout_gravity="center"
        android:background="#f2bcbc"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 2\nLayout weight = 1"
        android:layout_gravity="center"
        android:background="#bccff2"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 3\nLayout weight = 1"
        android:layout_gravity="center"
        android:background="#cef2bc"
        android:gravity="center"
        android:layout_weight="1"
        />
</LinearLayout>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8d938b"
    android:weightSum="3"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="LinearLayout Weight Sum = 3\nTextView 1, Layout weight = 1"
        android:layout_gravity="center"
        android:background="#779980"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 2\nLayout weight = 1"
        android:layout_gravity="center"
        android:background="#5993ff"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="TextView 3\nLayout weight = 1\nSecond line\nThird line\nFourth line\nSixth line"
        android:layout_gravity="center"
        android:background="#ff4d00"
        android:gravity="center"
        android:layout_weight="1"
        />
</LinearLayout>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8d938b"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="weight = 1"
        android:layout_gravity="center"
        android:background="#576c91"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="weight = 1"
        android:layout_gravity="center"
        android:background="#b68e7c"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="wrap_content"
        android:layout_gravity="center"
        android:background="#834e8f"
        android:gravity="center"
        />
</LinearLayout>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8d938b"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="wrap_content"
        android:layout_gravity="center"
        android:background="#ff4e02"
        android:gravity="center"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:text="weight = 1, height = 0dp"
        android:layout_gravity="center"
        android:background="#7ca6b6"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="wrap_content"
        android:layout_gravity="center"
        android:background="#d23dc6"
        android:gravity="center"
        />
</LinearLayout>
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linear_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#8d938b"
    android:weightSum="5"
    >
    <TextView
        android:id="@+id/tv1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="weightSum = 5, weight = 2"
        android:layout_gravity="center"
        android:background="#a8b988"
        android:gravity="center"
        android:layout_weight="2"
        />
    <TextView
        android:id="@+id/tv2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="weight = 1,"
        android:layout_gravity="center"
        android:background="#90e0ff"
        android:gravity="center"
        android:layout_weight="1"
        />
    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="weight = 2,"
        android:layout_gravity="center"
        android:background="#6f68d1"
        android:gravity="center"
        android:layout_weight="2"
        />
</LinearLayout>