ConstraintLayout是否比Android的RelativeLayout提供任何优势 - 或者ConstraintLayout是否只会增加不必要的复杂性?
这是我正在构建的示例布局,代码段如下:Android Layout Screenshot
在我看来,RelativeLayout产生的结果相同,但是:
我是否错过了相对于RelativeLayout的ConstraintLayout优势?
这是ConstraintLayout代码:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_patient_home"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doclily.doclily.MessagesTab"
android:background="#fff" >
<com.doclily.doclily.style.ViewGrey4
android:id="@+id/navBarDummyView"
android:layout_width="0dp"
android:layout_height="1dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="2dp"
app:layout_constraintTop_toTopOf="@+id/navBarGuideline" />
<android.support.constraint.Guideline
android:id="@+id/navBarGuideline"
android:orientation="horizontal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
app:layout_constraintGuide_begin="@dimen/action_bar_height"
tools:layout_editor_absoluteY="48dp"
tools:layout_editor_absoluteX="0dp"/>
<com.doclily.doclily.style.TextViewRegularBlack
android:id="@+id/MessagesTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/action_bar_margin_left"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintBottom_toTopOf="@+id/navBarDummyView"
app:layout_constraintTop_toTopOf="parent"
android:text="Messages"
android:textSize="@dimen/base_font_size_24"/>
<com.doclily.doclily.style.TextViewRegularBlack
android:id="@+id/placeholderTextView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Placeholder TextView"
android:layout_marginLeft="16dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/navBarDummyView"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:textSize="@dimen/base_font_size_18"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/messagesSwipe"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/navBarDummyView">
<android.support.v7.widget.RecyclerView
android:id="@+id/threadsTableRecyclerView"
tools:listitem="@layout/item_messaging_tab_row"
app:layoutManager="LinearLayoutManager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.constraint.ConstraintLayout>
这是RelativeLayout代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/activity_patient_home"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.doclily.doclily.MessagesTab"
android:background="#fff" >
<com.doclily.doclily.style.TextViewRegularBlack
android:id="@+id/MessagesTitle"
android:layout_width="match_parent"
android:layout_height="@dimen/action_bar_height"
android:layout_marginStart="@dimen/action_bar_margin_left"
android:gravity="center_vertical"
android:text="Messages"
android:textSize="@dimen/base_font_size_24"/>
<com.doclily.doclily.style.ViewGrey4
android:id="@+id/navBarDummyView"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="@+id/MessagesTitle"
android:layout_marginTop="2dp"/>
<com.doclily.doclily.style.TextViewRegularBlack
android:id="@+id/placeholderTextView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Placeholder TextView"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:textSize="@dimen/base_font_size_18"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/messagesSwipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/navBarDummyView">
<android.support.v7.widget.RecyclerView
android:id="@+id/threadsTableRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:listitem="@layout/item_messaging_tab_row"
app:layoutManager="LinearLayoutManager"/>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>
答案 0 :(得分:1)
ConstraintLayout最适合构建响应式布局。
我建议你阅读官方指南
ConstraintLayout允许您使用平面视图层次结构创建大型复杂布局(无嵌套视图组)。它与RelativeLayout类似,因为所有视图都是根据兄弟视图和父布局之间的关系布局的,但它比RelativeLayout更灵活,更易于使用Android Studio的布局编辑器。