How to Use android:layout_weight in LinearLayout

In this android programming source code example, we going to explore how to use android:layout_weight in LinearLayout.

Below is a step by step source code to use android:layout_weight in android LinearLayout.

activity_layout6.xml

<?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"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".layout.Layout6">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:background="@color/colorPrimary"/>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/colorAccent"/>

</LinearLayout>

Layout6.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class Layout6 extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_layout6);
    }
}

 

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment