我在屏幕上有很多项目,我需要使用滚动条,以便用户可以向下滚动。然而,滚动要么不可见,要么不起作用。怎么可能添加一个滚动条到线性布局?
当前回答
用<ScrollView>包装线性布局
请看下面的例子:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
注意:fill_parent已弃用,并在API Level 8中重命名为match_parent 甚至更高。
其他回答
下面是我通过试错的方法做到的。
ScrollView - (the outer wrapper).
LinearLayout (child-1).
LinearLayout (child-1a).
LinearLayout (child-1b).
由于ScrollView只能有一个子元素,所以该子元素是线性布局。然后所有其他布局类型都出现在第一个线性布局中。我还没有尝试包含一个相对布局,但它们让我发疯,所以我要等到我的理智回来。
你可以在linearLayout: android:scrollbars="vertical"中添加一个属性
你需要把ScrollView作为布局文件的第一个子文件,现在把你的线性布局放在里面。现在,android将根据可用的内容和设备大小决定是否显示滚动。
确保线性布局没有兄弟,因为ScrollView不能有一个以上的子。
无论何时你想让一个布局可滚动,你可以使用<ScrollView>与布局或组件在其中。
您需要使用以下属性并将其包含在线性布局中
<LinearLayout ...>
<scrollView ...>
</scrollView>
</LinearLayout>
推荐文章
- 改变开关的“开”色
- 以编程方式将EditText的输入类型从PASSWORD更改为NORMAL,反之亦然
- 如何在隐藏和查看密码之间切换
- 在Android上调整一个大的位图文件到缩放输出文件
- 如何更改Android版本和代码版本号?
- Android Studio突然无法解析符号
- 应用程序重新启动而不是恢复
- 如何设置整个应用程序在纵向模式?
- Android中文本的阴影效果?
- 以编程方式设置TextView的布局权重
- Android -如何覆盖“后退”按钮,所以它不完成()我的活动?
- 如何从通知点击发送参数到一个活动?
- 导航目标xxx对于这个NavController是未知的
- 使用ConstraintLayout均匀间距的视图
- 文件google-services错误。模块根文件夹中缺少Json。谷歌服务插件没有它就不能正常工作。