在ConstraintLayout中使用merge,在网上查看了好多,布局都不能正确的解决我的问题。经过我的一番研究后,我已经使用merge上瘾了。
现在将代码经验总结如下:
总布局:
<include
layout="@layout/dialog_title_bar"/>
<Button
android:id="@+id/tv_date"
android:layout_width="match_parent"
android:layout_height="@dimen/list_menu_item_height"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/cancel"/>
此处注意:include无需为其设置id,button 也不要相对于include进行布局,要做到仿佛被引用的布局存在一样。所以 button中,app:layout_constraintTop_toBottomOf="@id/cancel", cancel是被引用的布局中的id。
注意:include里面无需添加任何东西!Cool !
被引用的布局中,外层merge标签包裹即可。
<?xml version="1.0" encoding="utf-8"?>
<merge 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"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<TextView
android:id="@+id/cancel"
style="@style/text_16dp_black"
android:layout_width="wrap_content"
android:layout_height="@dimen/list_menu_item_height"
android:layout_marginStart="16dp"
android:gravity="center_vertical"
android:text="@string/cancel"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/title"
style="@style/text_16dp_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
app:layout_constraintBottom_toBottomOf="@+id/cancel"
app:layout_constraintEnd_toStartOf="@+id/confirm"
app:layout_constraintStart_toEndOf="@+id/cancel"
app:layout_constraintTop_toTopOf="@+id/cancel" />
<TextView
android:id="@+id/confirm"
style="@style/text_16dp_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@string/confirm"
app:layout_constraintBottom_toBottomOf="@+id/cancel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/cancel" />
</merge>
包裹后,记得加上: tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout"
否则你在AndroidStudio看到的子布局的预览图将是一个错位的布局(真机显示正常,外层布局正常)。
非常棒的引用方式,再次证明ConstraintLayout 的强大,可以和merge兼容的很好。
带领布局:
两个include联排,可以设置带领布局。带领布局可以是任意控件。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<View
android:id="@+id/control_position_of_merge_layout"
android:layout_width="match_parent"
android:layout_height="0.1dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<include
layout="@layout/merge_check_in_check_out"
/>
为merge_check_in_check_out 布局设置参考View。如上代码。
有了这个引导布局,我们的merge_check_in_check_out,就可以随意摆放了。
注意:merge_check_in_check_out中的所有组件,参考引导控件的位置。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<include
layout="@layout/dialog_title_bar"
android:layout_width="match_parent"
android:layout_height="@dimen/list_menu_item_height"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/control_position_of_merge_layout"
android:layout_width="match_parent"
android:layout_height="0.1dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cancel" />
<include layout="@layout/merge_check_in_check_out" />
</androidx.constraintlayout.widget.ConstraintLayout>
如上,即便是两个merge布局联排,也不怕。
网友评论