Android 开发学习ScrollView与constraintlayout嵌套实现支持滚动的约束布局

在学习中不断积累,在积累中不断学习

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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"
    android:overScrollMode="never"
    android:scrollbars="none"
    android:layout_margin="16dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        控件1

        控件2

        控件3
        .
        .
        .
        .
        .
        .

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

这个看上去很简单,自己添加控件进行相关控件的约束就行了,个人认为好处是不用考虑屏幕大小的问题,当你控件内容没有超过一屏高度时,会自动支持滚动效果~

其实从实际开发角度来说,应该是一屏能够展示下所有需要展示的内容最佳,看似这样的嵌套有些多余,不过实际使用中个人感觉还是有点用处,至少在你不确定你做的布局能否适配很多机型配置的时候,这个布局保证了所有内容都可以显示你要展示的全部内容。

这个中间遇到个小坑,之前的代码是这样的:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    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"
    android:overScrollMode="never"
    android:scrollbars="none">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp">

        控件1

        控件2

        控件3
        .
        .
        .
        .
        .
        .

    </androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>

仅仅是:android:layout_margin="16dp" 这句的位置不同,导致使用错误的代码布局时,当内容超过一屏,最下面的一个控件会显示不全,为此我百度了一个下午,最后还是自己调出来的,有些问题看上去很简单,只有当你自己出过这样的错误,并解决它,才会提高。

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值