Android 约束布局包括另一个布局
Android constraint layout include another layout
关于 ConstraintLayout
,我有更多的概念性问题。我想在我的 ConstraintLayout
中重新使用布局。但我问自己一个问题,ConstraintLayout
旨在避免布局嵌套,包含布局会降低 ConstraintLayout
性能。避免嵌套布局并同时重新使用布局以避免代码重复的最佳/良好做法是什么?
底线
不要害怕嵌套 ConstraintLayout
只要您的视图层次结构不会增长得太深 (<10)。我认为重用和嵌套布局以避免代码重复是一种很好的做法。
背景
我引用自the Android Developers page:
Additionally, if your app targets Android 7.0 (API level 24), it is likely that you can use a special layout editor to create a ConstraintLayout object instead of RelativeLayout. Doing so allows you to avoid many of the issues this section describes. The ConstraintLayout class offers similar layout control, but with much-improved performance. This class uses its own constraint-solving system to resolve relationships between views in a very different way from standard layouts.
开发者页面的 Optimizing Layout Hierarchies 提到
Deep layouts - Layouts with too much nesting are bad for performance. Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance. The default maximum depth is 10
然而,ConstraintLayout
并未在此页面上明确提及,我可能会补充说,深度为 10 的嵌套布局几乎肯定会导致相当混乱 UI。
补充一下,根据我作为软件开发人员的专业经验,尽管我的团队确实经常使用嵌套的 ConstraintLayout
(深度有限),但我从未遇到过 ConstraintLayout
的性能问题,我可能会补充),大部分时间都封装在自定义视图组件中。令人难过的是,这些事件当然不会发生过多,因为 ConstraintLayout
可以在不过度使用嵌套的情况下构建 UIs。
当一个布局包含在另一个布局中时,您可以使用 <merge/>
标签消除视图层次结构中的冗余视图组。
示例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<include layout="@layout/include_layout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
include_layout.xml
<?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">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</merge>
结果:
在这种情况下,如果没有 <merge/>
标记,布局层次结构中将有两个 ConstraintLayout
。
参考: https://developer.android.com/training/improving-layouts/reusing-layouts
关于 ConstraintLayout
,我有更多的概念性问题。我想在我的 ConstraintLayout
中重新使用布局。但我问自己一个问题,ConstraintLayout
旨在避免布局嵌套,包含布局会降低 ConstraintLayout
性能。避免嵌套布局并同时重新使用布局以避免代码重复的最佳/良好做法是什么?
底线
不要害怕嵌套 ConstraintLayout
只要您的视图层次结构不会增长得太深 (<10)。我认为重用和嵌套布局以避免代码重复是一种很好的做法。
背景
我引用自the Android Developers page:
开发者页面的Additionally, if your app targets Android 7.0 (API level 24), it is likely that you can use a special layout editor to create a ConstraintLayout object instead of RelativeLayout. Doing so allows you to avoid many of the issues this section describes. The ConstraintLayout class offers similar layout control, but with much-improved performance. This class uses its own constraint-solving system to resolve relationships between views in a very different way from standard layouts.
Optimizing Layout Hierarchies 提到
Deep layouts - Layouts with too much nesting are bad for performance. Consider using flatter layouts such as RelativeLayout or GridLayout to improve performance. The default maximum depth is 10
然而,ConstraintLayout
并未在此页面上明确提及,我可能会补充说,深度为 10 的嵌套布局几乎肯定会导致相当混乱 UI。
补充一下,根据我作为软件开发人员的专业经验,尽管我的团队确实经常使用嵌套的 ConstraintLayout
(深度有限),但我从未遇到过 ConstraintLayout
的性能问题,我可能会补充),大部分时间都封装在自定义视图组件中。令人难过的是,这些事件当然不会发生过多,因为 ConstraintLayout
可以在不过度使用嵌套的情况下构建 UIs。
当一个布局包含在另一个布局中时,您可以使用 <merge/>
标签消除视图层次结构中的冗余视图组。
示例:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
tools:context=".MainActivity">
<include layout="@layout/include_layout"/>
</androidx.constraintlayout.widget.ConstraintLayout>
include_layout.xml
<?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">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="33dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button" />
</merge>
结果:
在这种情况下,如果没有 <merge/>
标记,布局层次结构中将有两个 ConstraintLayout
。
参考: https://developer.android.com/training/improving-layouts/reusing-layouts