开发者

Trying to understand margins in LinearLayout inside a ScrollView

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>

Trying to understand margins in LinearLayout inside a 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>

Trying to understand margins in LinearLayout inside a 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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜