gpt4 book ai didi

java - 自定义 Horizo​​ntalScrollView 中的按钮

转载 作者:行者123 更新时间:2023-11-29 22:12:16 25 4
gpt4 key购买 nike

我想为计算器设计一个简单的界面。计算器有 2 组键,基本键和附加键。我想将额外的键放在 Horizo​​ntalScrollView 上。这是 xml 文件:

<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="D" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="E" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="F" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="G" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="H" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="J" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="K" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="L" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M" />
</LinearLayout>
</HorizontalScrollView>

这是它的样子:

enter image description here

但是,它工作得很好,我可以水平滚动并单击我想要的任何按钮。但我想更多地定制它。所以这是我的问题:

  • 如何编辑每个按钮的宽度,以便一次只能在屏幕上显示 5 个按钮。
  • 如何使滚动偏移 5 个按钮,即如果屏幕上出现最左边的 5 个按钮,然后向右滚动,那 5 个按钮将移至左侧,并显示接下来的 5 个按钮.
  • 如何使滚动条消失(即用不可见的滚动条滚动)。

谢谢你:)

最佳答案

如果您将 android:weightsum="5" 添加到您的线性布局,然后将 android:layout_weight="1" 添加到我认为的每个按钮 它会一次将其中的 5 个显示在屏幕上。但是我不太擅长使用 weight 属性,每当我使用它时,我都会进行一堆猜测和检查,但我的直觉告诉我,可以通过某种方式实现你所追求的 weight 和 weightsum。

但是,我想知道无论您在什么屏幕上始终显示 5 个按钮是否有意义?在我看来,你可能想要至少改变一些,这样最小的屏幕可能只有 4 个按钮,而最宽的屏幕可能有 6 个,否则你最终会得到非常广泛的按钮尺寸跨越所有不同的设备密度。

将此添加到您的 Horizo​​ntalScrollView:

android:scrollbars="false"

这应该会为您处理滚动条。

至于一次滚动 5 的部分,恐怕要实现这个你可能必须使用 horizo​​ntalScrollView.smoothScrollBy() 方法手动处理它,你必须做到计算出通过 5 个按钮需要走多远。然后覆盖您的 onTouch 监听器,为您调用 smoothScrollBy()

smoothScrollBy() - smoothScrollTo() 也值得一看。

关于java - 自定义 Horizo​​ntalScrollView 中的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9350043/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com