在学习中不断积累,在积累中不断学习
<?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" 这句的位置不同,导致使用错误的代码布局时,当内容超过一屏,最下面的一个控件会显示不全,为此我百度了一个下午,最后还是自己调出来的,有些问题看上去很简单,只有当你自己出过这样的错误,并解决它,才会提高。
本文探讨了在Android开发中,如何合理运用ScrollView与ConstraintLayout进行高效布局设计,避免常见错误,确保应用在不同设备上的一致性和美观性。
5294

被折叠的 条评论
为什么被折叠?



