ConstraintLayout约束布局,谷歌官方推荐的布局,Android2.3(API9)以后都可以使用此布局,与其他布局相比其可以很好的减少布局的层次,提升性能,结合RelativeLayout,LinearLayout等布局使用可以最大限度的减小界面的复杂度
大多人对这种布局还是比较陌生,熟练后你就会发现他的强大
ConstraintLayout的具体使用这里先不说了,网上有此内容,而且也是很简单的,只要你去用了,多用几次就很容易上手了
这里分享一个在使用ConstraintLayout布局遇到的问题
- 问题描述
异常现象:如上图界面,图2显示了底部导航栏后会将界面最底下部份遮住,此时布局中就算有ScrollView照样无法滑动显示,就是说此种布局状态下导航栏会将我们的内容遮住部份
正常现象:导航栏显示后会将整体界面上移,这是ScrollView就会有滑动效果了,内容也就不会被遮住
- 解决思路
1,layout_marginBottom 缺点:强制间距,不能兼容多类型手机
2,使用LinearLayout或RelativeLayout:经验证其他布局不会有此问题,缺点:回到老路了,我们是要使用约束布局
- 最终解决办法
简单...
在布局文件中加入下面的代码可以规避此问题
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="0dp" 官方推荐的写法,不再推荐使用match_parent
注意:上面两个属性得同时存在,不然不会生效
- 布局文件
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:zhy="http://schemas.android.com/tools">
<include
android:id="@+id/ic_title"
layout="@layout/activity_title"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="@+id/ic_title"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_height="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/tv_tip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:l