首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在安卓系统中为LinearLayout添加滚动条?

如何在安卓系统中为LinearLayout添加滚动条?
EN

Stack Overflow用户
提问于 2011-04-14 17:57:32
回答 2查看 52.2K关注 0票数 18

如何在Android中将滚动条添加到视图?

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

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

EN

回答 2

Stack Overflow用户

发布于 2011-04-14 18:05:24

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

只有ScrollViewHorizontalScrollViewListViewGridViewExpandableListView等可滚动容器才会显示滚动条。

我建议你把你的LinearLayout放在一个ScrollView里面,如果有足够的内容可以滚动,默认情况下会显示垂直滚动条。

代码语言:javascript
复制
<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

票数 55
EN

Stack Overflow用户

发布于 2011-04-14 18:03:20

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

代码语言:javascript
复制
<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>

如果您想在代码中完成此操作:

代码语言:javascript
复制
ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);
票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5661486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档