【问题标题】:layout_weight does not work when the layout inside a ScrollView is larger than the ScrollView当 ScrollView 内的布局大于 ScrollView 时,layout_weight 不起作用
【发布时间】:2013-06-26 20:51:17
【问题描述】:

我正在尝试使用线性布局的 layout_weight 作为滚动视图内部的高度。但是我发现 layout_weight 仅适用于 fillViewPort=true 并且如果有足够的剩余空间供 linear_weight 扩展。

我在下面列出了三个示例,我想让 textView 在 #2 和 #3 中的大小与 xml 级别中的 #1 相同。 正如您在链接中看到的,在 #2、#3 中,textView 已自动更改。

这是我的例子。 TextViews 和 Buttons 包含在 LinearLayout 中,然后包含在 scrollView 中。

1。 Layout_weight 相对于父级工作得很好。 (工作)

2。 Layout_weight 相对于剩余空间而不是父空间起作用。(不工作)

3 Layout_weight 被忽略(不工作)

这是我#3 的 xml 代码。 #2 和 #1 基本上是相同的代码,按钮较少或没有按钮。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scroller"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:weighSum="1"
    android:fillViewport="true" >

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

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />

        <Button
            android:layout_width="30dip"
            android:layout_height="30dip"
            android:text="Accept" />


        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="6"
            android:background="#AAABBB"
            android:text="hello" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="3"
            android:background="#BBBAAA"

            android:text="hello" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="5"
            android:background="#AAABBB"

            android:text="hello" />
    </LinearLayout>

</ScrollView>

【问题讨论】:

  • (工作案例)s22.postimg.org/v0gl2tmyp/…
  • 这三个场景可以加xml吗?这将有助于更清楚地说明您要完成的工作。
  • 究竟是什么不适合您?还是您希望其他人完成整个布局?
  • 嗨,我只想让 layout_weight 工作。但我认为 layout_weight 不起作用,因为它操纵其孩子的高度的滚动视图的性质。所以我只是想看看我是否错了,对于场景 #2 和 #3 有一个解决方法。谢谢。
  • 我认为您正在寻找的解决方法是在“加权”视图上设置 minHeight 属性。请参阅下面的答案。

标签: android android-layout android-linearlayout android-xml android-scrollview


【解决方案1】:

权重按设计工作。也许不是您希望他们“工作”的方式,但说他们“不工作”是不准确的。考虑一下您的第三种情况,您希望 TextView 的像素大小是多少?权重是可用空间的百分比(不是父级的总大小)。滚动视图和线性布局至少是屏幕的大小,否则是所有子视图的总和。因此,如果您的子视图超过屏幕大小(没有权重的视图),您希望为剩余的视图(具有 0dp 和权重的视图)分配多少空间?关键是空间的大小是任意的,android不会尝试假设你想要多少空间,因为它不可能知道。

TL;DR - 在您的文本视图上设置一个 minHeight(以 dp 为单位)以处理按钮过多的情况。

【讨论】:

  • 是的,你是对的。我查看了线性布局的源代码,它仅适用于剩余空间,任何超出空间的布局都将设置为 wrap_content。感谢您的答复。我希望有更好的方法来操作 ScrollView 中的子布局。
【解决方案2】:

这些图像正是我希望在您的 xml 中看到的。

这可能会对您有所帮助:

  1. 当系统使用权重方法将Views 布局到屏幕上时,它只根据权重分配 spare 空间,而不是 total空间。因此,要让它根据权重分配总空间(这是大多数人直觉上所期望的),您必须将您希望它工作的维度设置为 0dip。所以,对于那些Buttons 和TextViews 你需要设置

    android:layout_height = "0dip"
    
  2. 您可能还需要强制 ScrollView 填充视口,以阻止所有内容出现混乱。见here

    android:fillViewport="true"
    
  3. 个人观察。有时权重似乎相反(即较小的权重提供更多空间)。 YMMV。

【讨论】:

  • 您好,感谢您的回复。我把按钮放在那里是为了让里面的布局更大,没有其他原因。我只对使 textViews 与父布局成比例感兴趣。另外,我编辑了代码,我不小心粘贴了错误的代码,但无论如何,案例 #3 都不起作用。
  • 我有点困惑。您为布局 3 显示的图片正是我期望 xml 提供的。你期待什么?
  • 我希望 textViews 相对于权重及其父级增长。所以 textView #1 会增长 60%,textView #2 会增长 30%,textView #3 会增长 50%。
  • 那么,您是否尝试按照我的建议将TextViews 和Buttons 的layout_height 设置为"0dip"
  • 是的,我有,但它不起作用。只要我的 LinearLayout 不大于 scrollView,该方法就可以工作。
【解决方案3】:

尝试在具有 layout_weight 的元素上设置 layout_height="0dp"。封闭的布局对象将使用任何可用空间来尽可能多地增长它们。

您对 weightSum 的使用也有点问题。通常 weightSum 应该 >= 封闭视图的总权重。您有一个 weightSum,其中没有一个直接封闭的视图具有权重,另一个是封闭视图的总权重超过 weightSum。第二种情况会导致奇怪的行为。

【讨论】:

  • 您好,感谢您的回复。我的视图总和权重大于 weightsum 是因为要测试它是否可以使 ScrollView 中的虚拟维度更大。然而,事实证明 layout_weight 只能使用剩余可用空间。所以超出的范围将被裁剪。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多