Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

layout_weight attribute in <include> tag

Tags:

android

I'm trying to reuse some layout components in android application using <include> tag. I've got different port and land layouts:

  1. Port:

    <LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
    
        <include layout="@layout/calc_equals_button" a:layout_weight="4"/>
        <include layout="@layout/calc_display"/>
    
    </LinearLayout>
    
  2. Land:

    <LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
    
        <include layout="@layout/calc_equals_button"/>
        <include layout="@layout/calc_display"/>
    
    </LinearLayout>
    

Main difference is a:layout_weight="4", so i want my calc_equals_button component to be smaller in port orientation.

The thing is if i try to embed calc_equals_button component directly everything works OK, e.g.:

<LinearLayout a:layout_weight="1" a:layout_width="match_parent" a:layout_height="0dp">
        <DirectionDragButton
                xmlns:a="http://schemas.android.com/apk/res/android"
                a:id="@+id/equalsButton"
                a:text="="
                a:layout_width="match_parent"
                a:layout_height="match_parent"
                a:layout_weight="4"
                style="@style/control_button_style"
                a:onClick="numericButtonClickHandler"/>

        <include layout="@layout/calc_display"/>

    </LinearLayout>

otherwise - NOT.

Here is sample of calc_equals_button.xml:

<DirectionDragButton
    xmlns:a="http://schemas.android.com/apk/res/android"
    a:id="@+id/equalsButton"
    a:text="="
    a:layout_width="match_parent"
    a:layout_height="match_parent"
    style="@style/control_button_style"
    a:onClick="numericButtonClickHandler"/>
like image 358
se.solovyev Avatar asked Oct 08 '11 10:10

se.solovyev


People also ask

What is layout_weight and weightSum in android?

android:weightSum. Defines the maximum weight sum. If unspecified, the sum is computed by adding the layout_weight of all of the children. This can be used for instance to give a single child 50% of the total available space by giving it a layout_weight of 0.5 and setting the weightSum to 1.0.

How to make LinearLayout horizontal?

To create a linear layout in which each child uses the same amount of space on the screen, set the android:layout_height of each view to "0dp" (for a vertical layout) or the android:layout_width of each view to "0dp" (for a horizontal layout). Then set the android:layout_weight of each view to "1" .

What is android layout_weight?

In a nutshell, layout_weight specifies how much of the extra space in the layout to be allocated to the View. LinearLayout supports assigning a weight to individual children. This attribute assigns an "importance" value to a view, and allows it to expand to fill any remaining space in the parent view.


1 Answers

A current limitation of is that you must specify layout_width and layout_height for other layout_* attributes to be applied.

like image 139
Romain Guy Avatar answered Nov 07 '22 15:11

Romain Guy