界面需求描述:界面顶部标题栏,中间可滑动视图,底部固定布局,中间可滑动的视图在标题栏下方和底部视图上方展现。 刚开始的视图文件布局大概如下:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/liveGoodsAddRootLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <include android:id="@+id/titleLayout" layout="@layout/layout_titleBar" app:layout_constraintStart_toStartOf="parent" /> <androidx.core.widget.NestedScrollView android:id="@+id/contentNestedScroll" android:layout_width="0dp" android:layout_height="wrap_content" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/titleLayout" app:layout_constraintBottom_toTopOf="@+id/addGoodsTv" > // 省略这里的内容视图 </androidx.core.widget.NestedScrollView> <TextView android:id="@+id/addGoodsTv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_10" android:layout_marginTop="@dimen/dp_15" android:layout_marginRight="@dimen/dp_10" android:layout_marginBottom="@dimen/dp_15" android:background="@drawable/bg_supper_color" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:gravity="center" android:onClick="onClick" android:text="即刻添加" android:textColor="@color/white" android:textSize="@dimen/sp_14" /> </androidx.constraintlayout.widget.ConstraintLayout>这样titleLayout,contentNestedScroll,addGoodsTv 这三个视图无论我怎样约束,都显示异常,达不到想要的效果,后来查资料搞定,查看这里 ConstraintLayout Doc 相关。 注意看下图关于尺寸的描述,关键最后两行,关于wrap_content和0dp的描述: 然后更改视图布局NestedScrollView的android:layout_height=“0dp”,果真这样高度就充满在约束条件内,在标题栏和底部视图之间滑动,达到理想效果。
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/liveGoodsAddRootLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white"> <include android:id="@+id/titleLayout" layout="@layout/layout_titleBar" app:layout_constraintStart_toStartOf="parent" /> <androidx.core.widget.NestedScrollView android:id="@+id/contentNestedScroll" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@+id/titleLayout" app:layout_constraintBottom_toTopOf="@+id/addGoodsTv" > // 省略这里的内容视图 </androidx.core.widget.NestedScrollView> <TextView android:id="@+id/addGoodsTv" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginLeft="@dimen/dp_10" android:layout_marginTop="@dimen/dp_15" android:layout_marginRight="@dimen/dp_10" android:layout_marginBottom="@dimen/dp_15" android:background="@drawable/bg_supper_color" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" android:gravity="center" android:onClick="onClick" android:text="即刻添加" android:textColor="@color/white" android:textSize="@dimen/sp_14" /> </androidx.constraintlayout.widget.ConstraintLayout>最后想说ConstraintLayout 效率高,灵活性强,真的棒!!!ConstraintLayout Doc 相关
