如何在 Android 中向 LinearLayout 添加滚动条?

发布于 2024-11-01 09:59:50 字数 194 浏览 0 评论 0原文

如何向 Android 视图添加滚动条?

我尝试将 android:scrollbars:"vertical" 添加到布局 XML 文件中的 LinearLayout 中,但它不起作用。

我以为滚动条是在 Android 中默认绘制的,但似乎并非如此。看来我们必须自己画它——我该怎么做呢?

How do I add scrollbars to a view in Android?

I tried by adding android:scrollbars:"vertical" to the LinearLayout in my layout XML file but it is not working.

I thought scrollbars were drawn by default in Android but it does not seem that way. It seems we have to draw it ourselves - how do I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谜泪 2024-11-08 09:59:50

您无法将滚动条添加到 LinearLayout,因为它不是可滚动容器。

仅显示可滚动容器,例如 ScrollViewHorizo​​ntalScrollViewListViewGridViewExpandableListView滚动条。

我建议您将 LinearLayout 放置在 ScrollView 中,如果有足够的内容可以滚动,默认情况下它将显示垂直滚动条。

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

        <!-- Your content goes here -->   

    </LinearLayout>
</ScrollView>

如果您希望始终显示垂直滚动条,请将 android:scrollbarAlwaysDrawVerticalTrack="true" 添加到 ScrollView 中。请注意,LinearLayout 的高度设置为 wrap_content - 这意味着 LinearLayout 的高度可以大于 ScrollView 的高度 如果有足够的内容 - 在这种情况下,您将能够上下滚动 LinearLayout

You cannot add scrollbars to a LinearLayout because it is not a scrollable container.

Only scrollable containers such as ScrollView, HorizontalScrollView, ListView, GridView, ExpandableListView show scrollbars.

I suggest you place your LinearLayout inside a ScrollView which will by default show vertical scrollbars if there is enough content to scroll.

<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

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

        <!-- Your content goes here -->   

    </LinearLayout>
</ScrollView>

If you want the vertical scrollbar to always be shown, then add android:scrollbarAlwaysDrawVerticalTrack="true" to your ScrollView. Note the height of the LinearLayout is set to wrap_content - this means the height of the LinearLayout can be larger than that of the ScrollView if there is enough content - in that case you will be able to scroll your LinearLayout up and down.

遗忘曾经 2024-11-08 09:59:50

您无法以这种方式向小部件添加滚动条。您可以将小部件包装在 ScrollView 中。这是一个简单的例子:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt"/>
    </LinearLayout>
</ScrollView>

如果你想用代码来做:

ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);

You can't add a scrollbar to a widget that way. You can wrap your widget inside a ScrollView. Here's a simple example:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt"/>
    </LinearLayout>
</ScrollView>

If you want to do it in code:

ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文