Question

I have a linear layout which has 4 childs with different weights. When user click a button at first child , i want to add new child to parent linear layout with different weight ( 0dp height ) . But when i am adding new child all childs height is changing.How to add new view without change height percantage ?

    ------
    view 1 (25%)
    ------  
    ------
    view 2 (25%)
    ------  
    ------
    view 3 (25%)
    ------  
    ------
    view 4 (25%)
    ------  

after adding new layout programmatically

     ------
    view 1 (25%)
    ------  
    ------
    view 2 (25%)
    ------  
    ------
    view  3 (25%)
    ------  
    ------
    view 4 (25%)
    ------   
    ------
    view 5 (25%)
    ------    

total layout will be 125% and scrollable. Thanks

Was it helpful?

Solution

When you use weights, the sizes of your views are determined dynamically to ensure that they all views fit within the size of the parent - with proportions determined by the weights.

From what you described, a LinearLayout does not sound like the correct solution to your problem. A ListView may be a better approach, which will automatically provide the scrolling functionality when the total size of all items exceeds the size of the ListView itself. ListView is easy to implement and it gives you scrolling and separators between items automatically. Have a look at ListView developer's guide for more information.

OTHER TIPS

Yes you can use Custom style listview for autoscrolling and getting expected look and feel. You can use like this:

<ListView
                android:id="@+id/"
                android:layout_width="200dp"
                android:layout_height="wrap_content" 
                android:divider="@color/off_white"
                android:dividerHeight="2dp"/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top