Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Layout Weights do not work inside a ScrollView

I want to assign layout weights to several items within a LinearLayout inside of a ScrollView. However, the ScrollView ignores the LinearLayout weightSum.

My goal is to divide the layout with weights of 2, 1, 1 (for a total sum of 4), but this does not work properly inside of a ScrollView.

How can I solve this layout problem?

main.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/scrollView1"     android:layout_width="fill_parent"     android:layout_height="fill_parent">      <LinearLayout          android:layout_width="fill_parent"         android:layout_height="fill_parent"         android:orientation="vertical"          android:weightSum="4">          <LinearLayout android:id="@+id/logo"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:orientation="vertical"              android:layout_weight="2"             android:background="#FFFFFF" />          <LinearLayout android:id="@+id/logo1"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:orientation="vertical"              android:layout_weight="1"             android:background="#000000" />           <LinearLayout android:id="@+id/logobutton"             android:layout_width="fill_parent"             android:layout_height="wrap_content"             android:orientation="horizontal"              android:layout_weight="1"              android:background="#4B4B4B" />      </LinearLayout> </ScrollView> 
like image 456
MIP TECH Avatar asked Apr 25 '12 08:04

MIP TECH


People also ask

What does layout weight do?

Layout Weight This attribute assigns an "importance" value to a view in terms of how much space it should occupy on the screen. A larger weight value allows it to expand to fill any remaining space in the parent view.

What is fillViewport in ScrollView?

fillViewport allows scrollView to extend it's height equals to the full height of device screen's height in the cases when the child of scroll view has less height.

Can we use ScrollView in RelativeLayout in Android?

ScrollView is a ViewGroup that make the view hierarchy placed within it scrollable. You may only need to place one other ViewGroup inside it as a child. To add many Views to ScrollView, just directly add them to the child ViewGroup such as: RelativeLayout, LinearLayout….


2 Answers

I have faced this problem before. Just use android:fillViewport="true" in your ScrollView and it will fill up the screen.

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/scrollView1"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:fillViewport="true" > 
like image 63
Akhil Avatar answered Sep 30 '22 21:09

Akhil


This won't work as you have done it. The child view of a ScrollView should be set to wrap_content. If you set it to fill_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.

The idea of layout_weight is to fill a specific area proportionately.

You should set all of the child LinearLayouts layout_height to either wrap_content or a specific size (in dp) and the parent LinearLayout layout_height to wrap_content

As said you need to remove the additional

xmlns:android="http://schemas.android.com/apk/res/android"

if it's not the root (first) element of the layout.

like image 25
David Scott Avatar answered Sep 30 '22 21:09

David Scott