尝试理解 ScrollView 内 LinearLayout 的边距

2024-03-07

我需要有一个LinearLayout里面一个SrollView然后LinearLayout必须有一个余量ScrollView。起初,我能想到解决这个问题的唯一方法就是LinearLayout在另一个里面LinearLayout在最后一个布局上设置边距。如果它们设置在外部,它们将无法工作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>

我的问题是:为什么我需要这样做?

如果我只有一个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:layout_margin="10dp"
        android:orientation="vertical"
        android:background="@color/layout_color_blue">
    </LinearLayout>
</ScrollView>

然后,搜索一些类似的问题,我发现了一些布局,这给了我在中使用填充的想法ScrollView而不是边距LinearLayout。这也解决了我的问题,我不需要LinearLayout在另一个里面。这是一个更优雅的解决方案。

不过,我想了解为什么内部的简单边距LinearLayout在内部时不起作用ScrollView。因为如果它不在 a 内,它确实可以正常工作ScrollView.

有人知道为什么吗?


我深入研究了源代码:

ScrollView延伸FrameLayout。这本身就存在一些保证金问题,并且ScrollView甚至没有试图解决这个问题。测量时基本上忽略边距。

但最终这并不重要,因为您应该能够在ScrollView本身(这是一个肯定,没有尝试过)。单个子视图不需要边距。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试理解 ScrollView 内 LinearLayout 的边距 的相关文章

随机推荐