【问题标题】:Layout Weights do not work inside a ScrollView布局权重在 ScrollView 中不起作用
【发布时间】:2012-04-25 08:48:15
【问题描述】:

我想将布局权重分配给 LinearLayoutScrollView 内的多个项目。但是,ScrollView 会忽略 LinearLayout weightSum

我的目标是将布局划分为 2、1、1 的权重(总和为 4),但这在 ScrollView 内无法正常工作。

如何解决这个布局问题?

ma​​in.xml

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" 
        android:weightSum="4">

        <LinearLayout android:id="@+id/logo"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="2"
            android:background="#FFFFFF" />

        <LinearLayout android:id="@+id/logo1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" 
            android:layout_weight="1"
            android:background="#000000" />


        <LinearLayout android:id="@+id/logobutton"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" 
            android:layout_weight="1" 
            android:background="#4B4B4B" />

    </LinearLayout>
</ScrollView>

【问题讨论】:

  • 检查这个:Android Layout Weight 我认为这是同样的问题。
  • 如果我只删除滚动视图而不是工作但我的问题是如果我使用滚动视图而不是工作

标签: android android-layout


【解决方案1】:

我以前遇到过这个问题。只需在 ScrollView 中使用android:fillViewport="true",它就会填满屏幕。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true" >

【讨论】:

  • @MIPTECH :试试这个答案。更多信息请访问 romain guy curious-creature.org/2010/08/15/scrollviews-handy-trick
  • 谢谢,我真的在寻找解决方案,这正是我所需要的。
  • 纵向效果很好,但横向效果不好,不能正常工作
  • 最佳解决方案。真的很有帮助。
【解决方案2】:

这不会像你所做的那样工作。 ScrollView 的子视图应设置为wrap_content。如果将其设置为fill_parent,它将填充ScrollView 的区域并且永远不会滚动,因为它不会大于ScrollView

layout_weight 的想法是按比例填充特定区域。

您应该将所有子 LinearLayouts layout_height 设置为 wrap_content 或特定大小(以 dp 为单位),并将父 LinearLayout layout_height 设置为 wrap_content

如上所述,您需要删除额外的

xmlns:android="http://schemas.android.com/apk/res/android"

如果它不是布局的根(第一个)元素。

【讨论】:

    【解决方案3】:

    根据我的经验,fillviewport=true 的工作方式有点奇怪。

    我通过将LinearLayoutFrameLayout 之类的另一个布局包装在一起解决了这个问题。

    我希望这会有所帮助。

    【讨论】:

    • 嗨,安迪。欢迎来到 StackOverflow。谢谢您的回答。但是这个答案在细节上非常薄弱。您应该展示一个示例来演示您提到的包装。请参阅这些Tips for answering。谢谢。
    【解决方案4】:

    在您的 ScrollView 中添加以下行,它将正常工作。

    ScrollView 必须只有一个孩子

    android:fillViewport="true"
    

    【讨论】:

      【解决方案5】:

      把它放在你的滚动视图中:

      android:fillViewport="true"

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 2020-03-13
        相关资源
        最近更新 更多