Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand margins in LinearLayout inside a ScrollView

Tags:

I need to have a LinearLayout inside a SrollView and that LinearLayout must have a margin from the ScrollView. At first, the only way I could think of to solve that issue was having a LinearLayout inside another LinearLayout with the margins set on the this last layout. They wouldn't work if they were set in the outer LinearLayout.

Example:

<?xml version="1.0" encoding="utf-8"?> <ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:fillViewport="true"     android:background="@color/layout_color_green">     <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:orientation="vertical"         android:background="@color/layout_color_yellow">         <LinearLayout             android:layout_width="match_parent"             android:layout_height="match_parent"             android:layout_margin="10dp"             android:orientation="vertical"             android:background="@color/layout_color_blue">         </LinearLayout>     </LinearLayout> </ScrollView> 

enter image description here

My question is: Why do I need to do this?

If I had only one LinearLayout there would be no margins...

Example:

<?xml version="1.0" encoding="utf-8"?> <ScrollView     xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="fill_parent"     android:layout_height="fill_parent"     android:orientation="vertical"     android:fillViewport="true"     android:background="@color/layout_color_green">     <LinearLayout         android:layout_width="match_parent"         android:layout_height="match_parent"         android:layout_margin="10dp"         android:orientation="vertical"         android:background="@color/layout_color_blue">     </LinearLayout> </ScrollView> 

enter image description here

Then, searching for some similar issue, I found a few layouts which gave me the idea of using padding in the ScrollView instead of margin in the LinearLayout. This also solves my problem and I don't need a LinearLayout inside another one. It's a more elegant solution.

Still, I would like to understand why the simple margin inside the LinearLayout doesn't work when inside a ScrollView. Because it does work fine if it's not inside a ScrollView.

Anyone knows why?

like image 945
rfgamaral Avatar asked Sep 24 '11 14:09

rfgamaral


People also ask

Can we use linear layout in ScrollView?

Here in this example of Linear Layout inside ScrollView we create a custom layout for user registration form using different views(TextView, EditText etc). After creating different views we enclose them inside Linear Layout and then we enclose the whole layout in ScrollView to make all the element or views scrollable.

Which XML attribute do you use to define the width of the LinearLayout inside the scrolling view?

When adding a LinearLayout inside a ScrollView , use match_parent for the LinearLayout android:layout_width attribute to match the width of the parent ScrollView , and use wrap_content for the LinearLayout android:layout_height attribute to make it only large enough to enclose its contents.

What is ScrollView explain how do you create ScrollView with the TextView and with LinearLayout?

In Android, a ScrollView is a view group that is used to make vertically scrollable views. A scroll view contains a single direct child only. In order to place multiple views in the scroll view, one needs to make a view group(like LinearLayout) as a direct child and then we can define many views inside it.


2 Answers

I digged a little bit into the source code:

ScrollView extends FrameLayout. This one has some margin issues itself and ScrollView is not even trying to solve that. The margins are basically ignored while measuaring.

But in the end it doesn't matter since you should be able to define a padding on the ScrollView itself (it's an affirmation, didn't try that). There should be no need for margin for a single child view.

like image 183
Knickedi Avatar answered Sep 17 '22 12:09

Knickedi


Hello Knickedi and Ricardo Amaral,

Though this answer is marked as solved but I want to put some lights on the issue.

As Knickedi said, ScrollView extends FrameLayout.

So My answer is that You can set the layout_gravity of LinearLayout within scrollView and then layout_margin will work in LinearLayout as case with linearLayout within FrameLayout.

I had same issue and I've applied this and it worked for me. :)

Example :

<ScrollView                 android:layout_width="fill_parent"                 android:layout_height="fill_parent">                  <LinearLayout                     android:layout_width="fill_parent"                     android:layout_height="wrap_content"                     android:layout_gravity="top"                     android:layout_marginTop="30dp"                     android:orientation="vertical" > </ScrollView> 
like image 25
hims_3009 Avatar answered Sep 17 '22 12:09

hims_3009