【问题标题】:Droid: Size buttons in horizontal scrollviewDroid:水平滚动视图中的大小按钮
【发布时间】:2012-12-14 23:08:41
【问题描述】:

我有一个表格布局,其中有 3 个按比例缩放以填充屏幕底部的按钮。我想用滚动视图替换这些按钮以适应更多按钮。我添加了一个滚动视图并添加了按钮,但它们的宽度已减小到宽度的 1/4,四个按钮宽。滚动视图工作正常,但我无法缩放它或按钮,并且在较大的显示器上,按钮在它们曾经缩放的地方仍然很小。

这是我的行代码:

    <TableRow
        android:id="@+id/TableRow5"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <HorizontalScrollView
            android:id="@+id/horizontalScrollView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_span="3"
            android:layout_weight="1"
            >

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:gravity="center"
                android:orientation="horizontal"
                android:weightSum="6" >

                <Button
                    android:id="@+id/button_low"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/sgbutton"
                    android:onClick="preLaunch"
                    android:padding="5dp"
                    android:tag="1"
                    android:text="@string/button_label_low"
                    android:textSize="16dp"
                    android:textStyle="normal" />

                <Button
                    android:id="@+id/button_high"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/sgbutton"
                    android:onClick="preLaunch"
                    android:padding="5dp"
                    android:tag="2"
                    android:text="@string/button_label_high"
                    android:textSize="16dp"
                    android:textStyle="normal" />

                <Button
                    android:id="@+id/button_practice"
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:layout_margin="5dp"
                    android:layout_weight="1"
                    android:background="@drawable/sgbutton"
                    android:onClick="preLaunch"
                    android:padding="5dp"
                    android:tag="3"
                    android:text="practice"
                    android:textSize="16dp"
                    android:textStyle="normal" />

            <Button
                android:id="@+id/Button012"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/sgbutton"
                android:padding="5dp"
                android:tag="1"
                android:text="@string/button_label_low"
                android:textSize="16dp"
                android:textStyle="normal" />

            <Button
                android:id="@+id/Button011"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/sgbutton"
                android:padding="5dp"
                android:tag="2"
                android:text="@string/button_label_high"
                android:textSize="16dp"
                android:textStyle="normal" />

            <Button
                android:id="@+id/Button013"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_margin="5dp"
                android:layout_weight="1"
                android:background="@drawable/sgbutton"
                android:padding="5dp"
                android:tag="3"
                android:text="practice"
                android:textSize="16dp"
                android:textStyle="normal" />
            </LinearLayout>
        </HorizontalScrollView>
    </TableRow>

如何让按钮缩放到显示宽度的 1/3?

【问题讨论】:

  • 尝试将每个按钮的layout_width 更改为"0dp",这样它们会更好地使用权重。另外,尝试将weightSum 更改为 3,保持每个按钮的权重为 1。
  • 好主意,没用。 ;)

标签: android scrollview


【解决方案1】:

这有点难以解释。

在测量子视图时,LinearLayout 将首先尝试根据子视图想要的大小将其空间划分给其子视图,然后根据它们的权重划分其余空间。

在划分空间之前,父 LinearLayout 应该知道它的宽度。但是在Horizo​​ntalScrollView 中,LinearLayout 不知道它应该有多大,它必须首先测量它的children view 以获得总和大小。在这种情况下,重量无法工作,因为没有剩余空间。 LinearLayout 的宽度等于 Button 的宽度之和。

我认为您必须在代码中设置宽度。将每个按钮的宽度设置为 screenWidth / 3。

【讨论】:

  • 在我看来,Android 的 xml UI 实现严重不足。为什么没有尺寸的百分比选项?如果我可以将我的按钮设置为屏幕宽度的 33%,并且包含 100% 的视图,那就很容易了。相反,我必须在代码中完成,打破了独立 xml 接口的想法。如果没有其他解决方案出现,我会将其标记为答案。谢谢。
  • 您可以使用权重作为百分比,但它是相对于父视图(LinearLayout)而言的。您可以使用 layout_width="match_parent" 将父视图的宽度设置为 100% 屏幕宽度,并且当您只有 3 个按钮时它可以工作。现在你想要有 6 个按钮,这意味着你希望父视图的宽度是屏幕宽度的 200%,这是不支持的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-08-19
  • 2014-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
相关资源
最近更新 更多