试图了解ScrollView内LinearLayout中的边距 [英] Trying to understand margins in LinearLayout inside a ScrollView
问题描述
我需要在SrollView
内有一个LinearLayout
,并且LinearLayout
必须与ScrollView
之间有一个边距.首先,我想解决该问题的唯一方法是在另一个LinearLayout
内放置一个LinearLayout
,并在最后一个布局上设置边距.如果将它们设置在外部LinearLayout.
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.
示例:
<?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>
我的问题是:我为什么需要这样做?
如果我只有一个LinearLayout
,就没有边距...
If I had only one LinearLayout
there would be no margins...
示例:
<?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>
然后,在寻找类似问题时,我发现了一些布局,这些布局使我有了在ScrollView
中使用填充而不是在LinearLayout
中使用空白的想法.这也解决了我的问题,并且我不需要在另一个内部插入LinearLayout
.这是一个更优雅的解决方案.
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.
仍然,我想了解为什么ScrollView
内的LinearLayout
内的简单边距不起作用.因为如果不在ScrollView
内,它也可以正常工作.
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
.
有人知道为什么吗?
推荐答案
我稍微深入了一下源代码:
I digged a little bit into the source code:
ScrollView
扩展FrameLayout
.这本身就存在一些边距问题,并且ScrollView
甚至没有尝试解决该问题.测量时,边距基本上被忽略.
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.
但是最后没关系,因为您应该能够在ScrollView
本身上定义填充(这是肯定的,没有尝试过).单子视图应该不需要保证金.
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.
这篇关于试图了解ScrollView内LinearLayout中的边距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!