如何将约束布局包含到另一个约束布局并在每个布局之间设置约束
How to include constraint layout to another constraint layout and set constraint between each
我正在使用 constraintLyout v 1.0.1。
我想在我的 xml 中包含一个子 ConstraintLayout 对应于我的全局布局的一部分(它本身就是一个 ConstraintLayout)。我将布局分成两个 xml 以便在其他地方使用这个子部分
我试过了,但我无法控制在父级中放置子约束布局的位置。我想知道我是否必须将所有内容都放在同一个 xml 文件中,或者它们是否是使用单独文件的解决方案。
tmp_1.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
<TextView
android:id="@+id/label_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL2"
app:layout_constraintStart_toStartOf="@id/label"
app:layout_constraintEnd_toEndOf="@id/label"
app:layout_constraintTop_toBottomOf="@id/label"
android:layout_marginTop="16dp"
/>
<include layout="@layout/tmp_2" />
</android.support.constraint.ConstraintLayout>
tmp_2.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/view_80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="80th element"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp"
android:layout_marginStart="12dp"
/>
</android.support.constraint.ConstraintLayout>
结果是这样
但我希望它是这样的
我试过了,但没用
<include
app:layout_constraintTop_toBottomOf="@id/label_2"
layout="@layout/tmp_2" />
居然找到了解决办法。
Android Studio 不会自动完成 include 标记中的 constraintLayout 参数,但只要您提供 include 大小,它们就会对其产生影响。
<include
layout="@layout/tmp_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_2"
/>
您可以在包含项中避免 ConstraintLayout
约束。我只是<include/>
原样。
MainActivity 布局文件:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbarLayout"
layout="@layout/layout_toolbar" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CONTENTS"
app:layout_constraintBottom_toBottomOf="@+id/footerLayout"
app:layout_constraintEnd_toEndOf="@+id/footerLayout"
app:layout_constraintStart_toStartOf="@+id/footerLayout"
app:layout_constraintTop_toTopOf="@+id/footerLayout" />
<include
android:id="@+id/footerLayout"
layout="@layout/layout_footer" />
</android.support.constraint.ConstraintLayout>
工具栏布局文件:
<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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/toolbarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/hidden"
android:textColor="@android:color/white"
tools:layout_editor_absoluteX="192dp"
tools:layout_editor_absoluteY="19dp" />
</android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
要包含一个约束布局并根据需要对其进行约束,必须像这样为包含的布局提供宽度和高度:
<include
android:id="@+id/shop_card_layout"
layout="@layout/shop_card_one"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@id/heading_tv"
app:layout_constraintTop_toBottomOf="@+id/heading_tv" />
要将一个约束布局包含到其他约束布局,以对包含布局的每个父级使用一个或多个约束布局...如下所示:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_landing_bg"
tools:context=".activity.DesignTestActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<include layout="@layout/common_footer_layout" />
</android.support.constraint.ConstraintLayout>
这是我的 xml 版面设计。享受代码。
如果使用 Constraint 布局,我们可以使用 layout tag。对于线性布局我们可以直接包含布局。
我有一个不同的要求,我在工具栏下方有一个选项卡视图。我已将工具栏高度设置为 0dp,并在选项卡视图下方添加了一个视图文件,以便选项卡视图和工具栏看起来是一个整体。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs_container"
style="@style/tab_layout_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/text_black_app"
app:tabTextColor="@color/txt_gray_contact_lbl" />
<LinearLayout
android:id="@+id/layout_elevation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tabs_container">
<include layout="@layout/gray_view_for_elevation" />
</LinearLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="100"
app:layout_constraintTop_toBottomOf="@+id/layout_elevation_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
此代码用于线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
style="@style/tab_layout_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/dp_4"
app:tabGravity="fill"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"/>
<include
android:id="@+id/layout_elevation_view"
layout="@layout/gray_view_for_elevation" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_gray" />
</LinearLayout>
我的立面视图文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#80bbbbbb" />
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#60bbbbbb" />
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#50bbbbbb" />
希望这会有所帮助。
我正在使用 constraintLyout v 1.0.1。
我想在我的 xml 中包含一个子 ConstraintLayout 对应于我的全局布局的一部分(它本身就是一个 ConstraintLayout)。我将布局分成两个 xml 以便在其他地方使用这个子部分
我试过了,但我无法控制在父级中放置子约束布局的位置。我想知道我是否必须将所有内容都放在同一个 xml 文件中,或者它们是否是使用单独文件的解决方案。
tmp_1.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
/>
<TextView
android:id="@+id/label_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LABEL2"
app:layout_constraintStart_toStartOf="@id/label"
app:layout_constraintEnd_toEndOf="@id/label"
app:layout_constraintTop_toBottomOf="@id/label"
android:layout_marginTop="16dp"
/>
<include layout="@layout/tmp_2" />
</android.support.constraint.ConstraintLayout>
tmp_2.xml
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="@+id/view_80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="80th element"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="10dp"
android:layout_marginStart="12dp"
/>
</android.support.constraint.ConstraintLayout>
结果是这样
但我希望它是这样的
我试过了,但没用
<include
app:layout_constraintTop_toBottomOf="@id/label_2"
layout="@layout/tmp_2" />
居然找到了解决办法。 Android Studio 不会自动完成 include 标记中的 constraintLayout 参数,但只要您提供 include 大小,它们就会对其产生影响。
<include
layout="@layout/tmp_2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/label_2"
/>
您可以在包含项中避免 ConstraintLayout
约束。我只是<include/>
原样。
MainActivity 布局文件:
<?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:layout_width="match_parent"
android:layout_height="match_parent">
<include
android:id="@+id/toolbarLayout"
layout="@layout/layout_toolbar" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CONTENTS"
app:layout_constraintBottom_toBottomOf="@+id/footerLayout"
app:layout_constraintEnd_toEndOf="@+id/footerLayout"
app:layout_constraintStart_toStartOf="@+id/footerLayout"
app:layout_constraintTop_toTopOf="@+id/footerLayout" />
<include
android:id="@+id/footerLayout"
layout="@layout/layout_footer" />
</android.support.constraint.ConstraintLayout>
工具栏布局文件:
<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:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
<TextView
android:id="@+id/toolbarTitleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/hidden"
android:textColor="@android:color/white"
tools:layout_editor_absoluteX="192dp"
tools:layout_editor_absoluteY="19dp" />
</android.support.v7.widget.Toolbar>
</android.support.constraint.ConstraintLayout>
要包含一个约束布局并根据需要对其进行约束,必须像这样为包含的布局提供宽度和高度:
<include
android:id="@+id/shop_card_layout"
layout="@layout/shop_card_one"
android:layout_height="wrap_content"
android:layout_width="300dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="@id/heading_tv"
app:layout_constraintTop_toBottomOf="@+id/heading_tv" />
要将一个约束布局包含到其他约束布局,以对包含布局的每个父级使用一个或多个约束布局...如下所示:
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/new_landing_bg"
tools:context=".activity.DesignTestActivity">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<include layout="@layout/common_footer_layout" />
</android.support.constraint.ConstraintLayout>
这是我的 xml 版面设计。享受代码。
如果使用 Constraint 布局,我们可以使用 layout tag。对于线性布局我们可以直接包含布局。
我有一个不同的要求,我在工具栏下方有一个选项卡视图。我已将工具栏高度设置为 0dp,并在选项卡视图下方添加了一个视图文件,以便选项卡视图和工具栏看起来是一个整体。
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabs_container"
style="@style/tab_layout_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:tabGravity="fill"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"
app:tabSelectedTextColor="@color/text_black_app"
app:tabTextColor="@color/txt_gray_contact_lbl" />
<LinearLayout
android:id="@+id/layout_elevation_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tabs_container">
<include layout="@layout/gray_view_for_elevation" />
</LinearLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHeight_percent="100"
app:layout_constraintTop_toBottomOf="@+id/layout_elevation_view" />
</androidx.constraintlayout.widget.ConstraintLayout>
此代码用于线性布局
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorWhite"
android:orientation="vertical">
<com.google.android.material.tabs.TabLayout
style="@style/tab_layout_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="@dimen/dp_4"
app:tabGravity="fill"
app:tabIndicatorFullWidth="false"
app:tabMode="scrollable"/>
<include
android:id="@+id/layout_elevation_view"
layout="@layout/gray_view_for_elevation" />
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background_gray" />
</LinearLayout>
我的立面视图文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#80bbbbbb" />
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#60bbbbbb" />
<View
android:layout_width="wrap_content"
android:layout_height="@dimen/dp_1"
android:background="#50bbbbbb" />
希望这会有所帮助。