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>
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>
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?
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.