我在屏幕上有很多项目,我需要使用滚动条,以便用户可以向下滚动。然而,滚动要么不可见,要么不起作用。怎么可能添加一个滚动条到线性布局?
当前回答
你可以在linearLayout: android:scrollbars="vertical"中添加一个属性
其他回答
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<---------Content Here --------------->
</LinearLayout>
</ScrollView>
</LinearLayout>
这可以使用标签<ScrollView>来完成。对于ScrollView,你需要提醒的是,ScrollView必须有一个子节点。
如果你想要你的整个布局是可滚动的,那么在顶部添加<ScrollView>。检查下面给出的例子。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
但是如果你想让布局的某些部分是可滚动的,那么在该部分中添加<ScrollView>。检查下面给出的例子。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="400dp">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- Content here -->
</LinearLayout>
</ScrollView>
</LinearLayout>
下面是我通过试错的方法做到的。
ScrollView - (the outer wrapper).
LinearLayout (child-1).
LinearLayout (child-1a).
LinearLayout (child-1b).
由于ScrollView只能有一个子元素,所以该子元素是线性布局。然后所有其他布局类型都出现在第一个线性布局中。我还没有尝试包含一个相对布局,但它们让我发疯,所以我要等到我的理智回来。
您需要使用以下属性并将其包含在线性布局中
<LinearLayout ...>
<scrollView ...>
</scrollView>
</LinearLayout>
你可以在linearLayout: android:scrollbars="vertical"中添加一个属性
推荐文章
- Android Studio, logcat在应用程序关闭后清理
- 在android中从上下文获取活动
- 无法解析主机"<URL here>"没有与主机名关联的地址
- getActivity()在Fragment函数中返回null
- 按钮背景是透明的
- 在Mac OS X上哪里安装Android SDK ?
- 我如何获得图像缩放功能?
- 在Android应用程序中显示当前时间和日期
- BottomSheetDialogFragment的圆角
- 在应用程序启动时出现“无法获得BatchedBridge,请确保您的bundle被正确打包”的错误
- 我如何改变默认对话框按钮的文本颜色在安卓5
- 更改单选按钮的圆圈颜色
- 如何在android中复制一个文件?
- adb找不到我的设备/手机(MacOS X)
- 如何在新的材质主题中改变背面箭头的颜色?