Chi Chi - 1 year ago 142
Android Question

LinearLayout with percent empty space

I am working on linear layout for my simple android application. I wanna make the portion of two views dynamically change based on the size ( I want to have, for a row for left to right, the first 20% is empty, and all the content is inside the rest of 80%) . For this approach, i chosen the weight for different view. I created an nested linear layout for this approach. For example, the layout hierarchy is something like this.

<linearLayout> //parent layout
<linearLayout //child 1 layout
//so that this view occupy 20% of the space regardless the width of device. I intensionally wanna keep this view empty.
<linearLayout //child 2 layout
//so that this view occupy 80% of the space regardless the width of device. and
//inside this view I have whatever view I wanna add on it.

With this approach, the Lint in Android Studio tell me the following warnings:

  1. This is a Nested Layout. Layout weights require a widget to be measured twice. When a LinearLayout with non-zero weights is nested inside another LinearLayout with non-zero weights, then the number of measurements increase exponentially.

  2. the child 1 layout is useless: This
    view is useless (no children, no
    , no
    , no

Can anyone address me the right layout to use in order to have the layout dynamically change based on the size of devices? How should I correctly set up the empty space for a linear layout case?

Answer Source

This is a possible solution using weights:


    <!-- Your content here: -->
        android:layout_gravity="end" />

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download