【问题标题】:How to always display 3 buttons horizontalscrollview如何始终显示 3 个按钮水平滚动视图
【发布时间】:2015-12-23 08:16:49
【问题描述】:

我正在使用带按钮的水平滚动视图。现在我希望在每种类型的移动屏幕上一次只显示 3 个按钮。怎么可能? 我正在使用以下代码。

<HorizontalScrollView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp" >

    <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="Button1" />

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

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

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

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

    </LinearLayout>
</HorizontalScrollView>

【问题讨论】:

  • each type of mobile screen 表示不同屏幕尺寸的手机?是吗
  • 意思是说在滚动之前总是应该显示3个按钮。
  • 你的意思是页面底部/顶部的 3 个按钮并在背景上滚动是吗?
  • 你能贴出你目前尝试过的截图吗?和你想要的截图

标签: android


【解决方案1】:

它在 xml 布局中是不可能的,因为你没有滚动视图的宽度但是...... 您可以通过以下代码获取设备宽度像素,然后将其划分为3:

DisplayMetrics display;
display = this.getResources().getDisplayMetrics();
int width = display.widthPixels; 

当您将设备宽度像素设为 3 并设置为按钮时:

btn.setWidth (width / 3);

希望对您有所帮助..

【讨论】:

    【解决方案2】:

    我想你正在寻找类似this.的东西

    您可以将按钮的大小设置为屏幕的 1/3(具有布局权重),我认为其余的按钮都不是“屏幕外”,因此您可以通过滚动使它们可见。 我不能告诉你代码的样子,因为我不是安卓开发者,但我做过一次,就是这样。

    希望能帮助你找到准确的解决方案!

    【讨论】:

      【解决方案3】:

      这很困难,因为 Horizo​​ntalScrollView 不会让你这样做。

      如果你使用 Horizo​​ntalScrollView 然后给这个按钮一个 weightSum ,它不会被应用。

      所以我会说,它取决于屏幕尺寸。如果你为一个修复它,那么它对另一个会有所不同。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 1970-01-01
        • 2017-02-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多