1

The xml layout is like this:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">
        </LinearLayout>
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="bottom"/>
    </LinearLayout>

And it turns out that the textview will be placed to the bottom of its parent.I think layout_weight is used to allocate unusesd space and it's very common to use layout_weight with code like :

    android:layout_width="0dp"

But in this case,the first layout asks to occupy all spaces with:

    android:layout_width="match_parent"
    android:layout_height="match_parent

So how does layout weight work here?
PS:I have read this question: What does android:layout_weight mean? but I don't think it accounts for this question.

layout_weight specifies how much of the extra space in the layout to be allocated to the View.

The first linearlayout has already taken the whole space with attribute match_parent,why setting layout_weight enables the second view to showup at the bottom ? I believe this is not the common usage of layout_weight.Hope somebody point out my mistake.

Community
  • 1
  • 1
programforjoy
  • 391
  • 3
  • 12
  • when you use layout_weight make layout_widht = '0dp' in horizontal orientation and make layout_height= '0dp' in vertical orientation – anonymous Jul 11 '16 at 04:17

2 Answers2

1

When you use layout_weight attribute it is used to calculate the weightage of child views of the single parent.

As you have not mentioned weight of all other views it's behaving wrong.

layout_weight is useful when you want your child views to be certain percentage of the parent view.

for example,

in parent view you need to mention:

android:weightSum="1"

So your parent view will have total weight as 1, and in both of the views you need to mention the:

android:layout_weight=".9" and android:layout_weight=".1"

so the first view will take 90%, and second view will take 10% of the space.

To be more clear Ideally the sum of weights of all the childs should be equal to the weightsum mentioned in parent, to it work as expected.

**And as you have provided android:layout_width,android:layout_height` of the textview, this is the mistake as it will make issue in the weight.

So to use weight attribute correctly you need to give other specs as 0dp in order to weight to apply successfully.**

Note: When you use weight other specs like android:layout_width, android:layout_height should be set to 0dp.

To understand it practically, why don't you play around with below layout:

Just Try to change the weight of linear_layout, text_view and you will see, how it's supposed to works ideally:

<?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:orientation="vertical"
    android:weightSum="100" >

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="10"
        android:background="@android:color/holo_blue_bright" >
    </LinearLayout>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="90"
        android:background="@android:color/white"
        android:text="bottom"
        android:textColor="@android:color/black" />

</LinearLayout>
Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61
  • Thanks for the answer.I understand the common use of layout_weight.What I intend to do at the beginning is to place the textview at the bottom of a linearlayout.The layout I posted in the question can achieve this goal.I am just wondering if someone can explain how it works. – programforjoy Jul 11 '16 at 04:56
  • i tried to explain the mistake, in the answer please read it and let me know if you still have any doubts, i would be glad to help you!! Cheers. – Shridutt Kothari Jul 11 '16 at 04:58
1

If you want to place your components in separate boxes in layout ,you should use LinearLayout. You can define the manner of boxes place with orientation vertical or horizental. You can define their size easily with layout_weight. look here:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id=@+id/parent_linear>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="4">
    </LinearLayout>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="bottom"/>
</LinearLayout>

parent_linear divides your layout in 2 parts(because you use 2 components) vertically. Now you can set weights to child's components width. So,(for TextView) you set android:layout_width="0dp" for its width and android:layout_weight="2" ..follow it for LinearLayout- . The result of this is parent_layout divide itself into 6 parts (2+4=6),and allocates 4 parts to LinearLayout and 2 for TextView.

Mina Dahesh
  • 332
  • 8
  • 21