<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.services.ServicesFragment">
,<!-- 这个里面包含了很多组件,超出了界面垂直长度,我该怎么修改,让它可以上下滚动-->
</androidx.constraintlayout.widget.ConstraintLayout>
android的constraintlayout布局怎么实现上下滚动,目前显示不完整,不能向下滚动查看超出界面的
外面套个NestedScrollView
约束布局ConstraintLayout
面世已有很长一段时间了,但我一直没有关注这个Android 中继五大布局后的新布局的使用。近日在网友的讨论的强烈推荐下,尝试了ConstraintLayout
。使用之后的最大感触就是:为什么我不早点在项目中尝试ConstraintLayout
!!!
本篇文章,旨在记录ConstraintLayout
的使用,方便日后查阅和学习,项目地址点这里。
首先我们概括的说一下ConstraintLayout
的优点。
从开发者的使用上来说:约束布局就是一个强力升级版的
RelativeLayout
,RelativeLayout
能实现的ConstraintLayout
也能实现,RelativeLayout
不能实现的ConstraintLayout
也能实现。从性能上来说:使用
ConstraintLayout
的布局基本上不存在ViewGroup
的多层级嵌套,我们知道View的绘制是从顶层开始,以遍历的形式完成整个界面的measure
、layout
、draw
。所以在复杂布局的绘制上ConstraintLayout
具有一定的性能优势。
ConstraintLayout
不仅功能更强大而且性能也比使用其他布局更好,我想这就是为什么Google
强烈推荐使用ConstraintLayout
的原因吧。而对我们开发者来说,有好用的东西就要积极拥抱。
答案:
要实现在ConstraintLayout布局中实现上下滚动,可以使用ScrollView套嵌ConstraintLayout的方式实现。具体步骤如下:
Step 1: 在app/grandle下添加ScrollView和ConstraintLayout的依赖
dependencies {
implementation 'com.android.support.constraint:constraint-layout:2.0.0-beta4'
implementation 'com.android.support:scrollView:28.0.0'
}
Step 2: 在布局文件中套嵌ScrollView和ConstraintLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
//添加需要滚动的View组件
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
Step 3: 对于需要滚动的View组件,需要设置相应的约束关系,确保它们可以正确的显示。例如:
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="我是需要滚动的文本"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
Step 4: 进行完上述步骤之后,即可在ConstraintLayout布局中实现上下滚动。
以上,就是在Android的ConstraintLayout布局中实现上下滚动的解决方案。