【问题标题】:HorizontalScrollView with Linear Layout not scrolling with scrollTo, scrollBy具有线性布局的 Horizo​​ntalScrollView 不使用 scrollTo、scrollBy 滚动
【发布时间】:2014-11-06 00:51:40
【问题描述】:

我无法让 Horizo​​ntalScrollView 滚动到某个 X 位置或 X 像素。它有一个线性布局,里面有按钮。

<HorizontalScrollView
        android:id="@+id/horizontal_room_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="0dp"
        android:scrollbars="none">

        <LinearLayout
            android:orientation="horizontal"
            android:id="@+id/roomsBar"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">

            <!-- buttons -->

        </LinearLayout>

</HorizontalScrollView>

Java:

buttonList = (HorizontalScrollView) findViewById(R.id.horizontal_room_list);
//do stuff, at some point successfully save the scrolling offset with getScrollX()
barOffsetX = buttonList.getScrollX();
Log.e("x", "" + barOffsetX); //this prints a correct > 0 value
buttonList.scrollBy(barOffsetX, 0); //nothing happens, or
buttonList.scrollTo(barOffsetX, 0); //nothing happens

我认为问题可能与 LinearLayout 有关。如何让它滚动?

编辑:我认为问题在于以编程方式添加新按钮。

【问题讨论】:

    标签: android xml android-linearlayout horizontal-scrolling horizontalscrollview


    【解决方案1】:

    您是否定义了固定子视图或线性布局,例如,按钮视图将接受 40 dp,因此您可以使用此方法将 dp-into-px 转换,并使用此方法的返回值。

    public static float convertDpToPixel(float dp, Context context) {
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi / 160f);
        return px;
    }
    

    【讨论】:

    • 遗憾的是,无论我给 scrollBy() 的任何值,scrollTo() 都不会改变任何东西:栏不会滚动。我忘了提到一件非常重要的事情(我编辑了我的问题):问题是按钮是从 java.util.d 动态添加的。由于某种原因,这会阻止栏滚动。目前,我通过关注实际按下的按钮“解决”了这个问题,但我不喜欢这个解决方案,因为我想将栏完全恢复为 ConfigurationChange 之前的状态。感谢您的回答,但问题不在于价值。
    猜你喜欢
    • 2016-05-31
    • 1970-01-01
    • 1970-01-01
    • 2018-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-09
    相关资源
    最近更新 更多