Topic : Layout Attribute - Understanding Weight Attribute:

Layout_Weight:

The Layout Attribute weight specify the size of Child views in a container. For example, if we want to place two buttons horizontally by occupying each half of the screen width, we can define layout weight as 0.5 for each button inside the Layout.


android:layout_weight="0.5"


Weight-attribute 50-50: infobrother

Example:

Let's design two Linear Layout vertically, The first one designed without weight attribute and the second one using weight Property.


WeightSum: In Order to use Weight Property, weightSum attribute is required. WeightSum is the sum of all the child attributes weight.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:weightSum="3">

    <Button
        android:layout_weight="1" />

    <Button
        android:layout_weight="1" />

    <Button
        android:layout_weight="1" />
</LinearLayout>

In Above Example, the sum of weight is 3.



<!-- Without Using Weight attribute -->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
<!-- vertical Orientation set -->

<!-- Child Views (In this case we have 3 Button) are here -->
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button1"
        android:id="@+id/button"
        android:background="#008080" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button2"
        android:id="@+id/button2"
        android:background="#08f9bd" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button3"
        android:id="@+id/button3"
        android:background="#f418d2" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="3"
    android:orientation="vertical">

<!-- Child Views (In this case we have 3 Button) are here -->
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button 1"
        android:background="#008080"
        android:layout_weight="1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button 2"
        android:background="#08f9bd"
        android:layout_weight="1" />

    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Button 3"
        android:background="#f418d2"
        android:layout_weight="1" />
</LinearLayout>



Using Weight Attribute Vs Not using weight attribute: infobrother

















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement