如何在Android中将滚动条添加到视图?
我尝试在我的布局XML文件中将android:scrollbars:"vertical"
添加到LinearLayout
中,但它不起作用。
我以为在Android中滚动条是默认绘制的,但看起来并非如此。似乎我们必须自己画它-我该怎么做呢?
发布于 2011-04-14 18:05:24
无法将滚动条添加到LinearLayout
,因为它不是可滚动容器。
只有ScrollView
、HorizontalScrollView
、ListView
、GridView
、ExpandableListView
等可滚动容器才会显示滚动条。
我建议你把你的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
。
发布于 2011-04-14 18:03:20
您不能以这种方式向小部件添加滚动条。您可以将小部件包装在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);
https://stackoverflow.com/questions/5661486
复制相似问题