Android ConstraintLayout我如何重用布局并仍然约束布局

当我想将通用布局代码重构为一个单独的xml文件时,我正在努力让ConstraintLayout为我工作。我可以使用RelativeLayouts轻松地做到这一点,但我相信RelativeLayouts已被弃用,我们现在应该使用ConstraintLayous。

我有一个带有屏幕的应用,该屏幕在纵向和横向上具有不同的布局,如下图所示。

Android ConstraintLayout我如何重用布局并仍然约束布局

我有2个具有相同名称的布局文件(“ my_layout.xml”),一个位于“ layout”文件夹中,另一个位于“ layout-land”中。目前,我已经复制了两个布局文件中的所有xml,并调整了约束,以便在横向版本中将视图水平放置。

肖像版

<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">

    <!-- OTHER VIEWS 1 -->
    <View
        android:id="@+id/OtherViews1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- OTHER VIEWS 2 -->
    <View
        android:id="@+id/OtherViews2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottom="@+id/OtherViews1"
        app:layout_constraintBottom_toTopOf="@+id/scrollbar" />


    <!-- SCROLL BAR VIEWS -->
    <TextView
        android:id="@+id/slow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStart_toStartOf="parent"/>
    <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/scrollbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3"
        app:layout_constraintStart_toEndOf="@+id/slow"
        app:layout_constraintEnd_toStartOf="@+id/fast"/>
    <TextView
        android:id="@+id/fast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStartEnd_toEndOf="parent" />


    <!-- OTHER VIEWS 3 -->
    <View
        android:id="@+id/OtherViews3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>    

横向版本

<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">

    <!-- OTHER VIEWS 1 -->
    <View
        android:id="@+id/OtherViews1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- OTHER VIEWS 2 -->
    <View
        android:id="@+id/OtherViews2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottom="@+id/OtherViews1"
        app:layout_constraintBottom_toBottomOf="parent" />


    <!-- SCROLL BAR VIEWS -->
    <TextView
        android:id="@+id/slow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintStart_toEndOf="@+id/OtherViews2"
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStart_toStartOf="parent"/>
    <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/scrollbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3"
        app:layout_constraintStart_toEndOf="@+id/slow"
        app:layout_constraintEnd_toStartOf="@+id/fast"/>
    <TextView
        android:id="@+id/fast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStartEnd_toEndOf="parent" />


    <!-- OTHER VIEWS 3 -->
    <View
        android:id="@+id/OtherViews3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content
        app:layout_constraintStart_toEndOf="@+id/OtherViews2""
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>    

两个文件中都复制了“慢速”文本框,滚动条和“快速”文本框的xml。我想将这3个元素的布局移到一个单独的文件中,并在两个“ my_layout.xml”文件中引用它,以便其更可重用,并且在布局文件之间没有重复。我想保持层次结构平坦(否则我将只使用RelativeLayouts)。我不知道如何为滚动条UI指定新的可重用xml布局文件的约束。

纵向版本重新使用滚动条布局

<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">

    <!-- OTHER VIEWS 1 -->
    <View
        android:id="@+id/OtherViews1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <!-- OTHER VIEWS 2 -->
    <View
        android:id="@+id/OtherViews2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottom="@+id/OtherViews1"
        app:layout_constraintBottom_toTopOf="@+id/scrollbar" />

    <include
        layout="@layout/scrollbar
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3"
        app:layout_constraintStart_toStartOf="parent"       />


    <!-- OTHER VIEWS 3 -->
    <View
        android:id="@+id/OtherViews3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>


</androidx.constraintlayout.widget.ConstraintLayout>    

可重复使用的滚动条布局

<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="wrap_content"
    android:layout_height="wrap_content">


    <!-- SCROLL BAR VIEWS -->
    <TextView
        android:id="@+id/slow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStart_toStartOf="parent"/>         ****************** 
    <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/scrollbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3" ****************** 
        app:layout_constraintStart_toEndOf="@+id/slow"
        app:layout_constraintEnd_toStartOf="@+id/fast"/>
    <TextView
        android:id="@+id/fast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ...
        app:layout_constraintTop_toTopOf="@+id/scrollbar"
        app:layout_constraintBottom_toBottomOf="@+id/scrollbar"
        app:layout_constraintStartEnd_toEndOf="parent" />       ****************** 

</androidx.constraintlayout.widget.ConstraintLayout>    

我不知道在标有****的行中要加什么内容。如果我指定“慢”文本框从父级的开头开始,则不适用于横向版本。我希望此滚动条布局仅指示慢速滚动条在左侧,中间的滚动条(占用所有剩余空间),而快速文本框在右侧。 我如何使用约束布局来做到这一点? 我还如何将所有3个视图垂直居中?

RelativeLayout非常容易,因为我会说慢速文本框是alignedParentLeft,快速是alignedParentRight,而滚动条在慢速文本框的右侧和快速文本框的左侧。最后,我想说所有3个视图在父级中垂直居中。

HELLO_JAVA521 回答:Android ConstraintLayout我如何重用布局并仍然约束布局

包含的布局中的视图不能引用包含该布局的布局文件中的视图。您仍然可以通过其他方法使用包含的布局。

使用快速/慢速文本和查找栏作为自封闭实体查看包含的布局-类似于以下内容:

scrollbar.xml

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/slow"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Slow"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatSeekBar
        android:id="@+id/scrollbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="@+id/slow"
        app:layout_constraintEnd_toStartOf="@+id/fast"
        app:layout_constraintStart_toEndOf="@+id/slow"
        app:layout_constraintTop_toTopOf="@+id/slow" />

    <TextView
        android:id="@+id/fast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fast"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

您现在可以将此布局包括在纵向和横向布局中:

activity_main.xml(肖像)

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp">

    <TextView
        android:id="@+id/OtherViews1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews1"
        app:layout_constraintBottom_toTopOf="@+id/OtherViews2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />

    <TextView
        android:id="@+id/OtherViews2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="24dp"
        android:layout_marginBottom="24dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews2"
        app:layout_constraintBottom_toTopOf="@+id/scrollbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottom="@+id/OtherViews1"
        app:layout_constraintTop_toBottomOf="@+id/OtherViews1"
        app:layout_constraintVertical_weight="4" />

    <include
        layout="@layout/scrollbar"
        android:id="@+id/scrollbar"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/OtherViews2"
        app:layout_constraintVertical_weight="0.5" />

    <TextView
        android:id="@+id/OtherViews3"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginTop="24dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews3"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/scrollbar"
        app:layout_constraintVertical_weight="1" />

</androidx.constraintlayout.widget.ConstraintLayout>

activity_main.xml(横向)

enter image description here

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="24dp">

    <TextView
        android:id="@+id/OtherViews1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews1"
        app:layout_constraintBottom_toTopOf="@+id/OtherViews2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_weight="1" />

    <TextView
        android:id="@+id/OtherViews2"
        android:layout_width="342dp"
        android:layout_height="0dp"
        android:layout_marginTop="24dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottom="@+id/OtherViews1"
        app:layout_constraintTop_toBottomOf="@+id/OtherViews1"
        app:layout_constraintVertical_weight="8" />

    <include
        android:id="@+id/scrollbar"
        layout="@layout/scrollbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        app:layout_constraintBottom_toTopOf="@+id/OtherViews3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/OtherViews3" />

    <!-- OTHER VIEWS 3 -->
    <TextView
        android:id="@+id/OtherViews3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:background="#FFCCCCCC"
        android:gravity="center"
        android:text="OtherViews3"
        app:layout_constraintBottom_toBottomOf="@+id/OtherViews2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/OtherViews2" />

</androidx.constraintlayout.widget.ConstraintLayout>

可以按照约束和大小将包含的布局文件视为其自己的窗口小部件。参见Re-using layouts with <include/>

本文链接:https://www.f2er.com/3156031.html

大家都在问