如何使用支持所有不同尺寸屏幕的 ConstraintLayout 对齐视图?

How to align view using ConstraintLayout that support all different size screens?

我正在使用 ConstraintLayout 来设计我的屏幕。

这是我到目前为止尝试过的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    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/home_bg">

    <ImageView
        android:id="@+id/topImage"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_marginTop="20dp"
        android:contentDescription="@string/app_name"
        android:src="@drawable/splash_heading"
        app:layout_constraintStart_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvAllOverResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:text="@string/allover_result"
        android:textColor="@color/colorBlue"
        android:textSize="20sp"
        android:textStyle="bold"
        ads:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/topImage" />

    <android.support.v7.widget.CardView
        android:id="@+id/firstCardView"
        android:layout_width="60dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="2dp"
        android:layout_marginEnd="10dp"
        android:layout_marginBottom="2dp"
        ads:cardElevation="5dp"
        app:layout_constraintBottom_toBottomOf="@+id/tvVs"
        app:layout_constraintEnd_toStartOf="@+id/tvVs"
        app:layout_constraintTop_toTopOf="@+id/tvVs">


        <abak.tr.com.boxedverticalseekbar.BoxedVertical
            android:id="@+id/firstSeekBar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_gravity="center"
            app:backgroundColor="@android:color/white"
            app:defaultImage="@drawable/ic_play"
            app:imageEnabled="true"
            app:max="100"
            app:maxImage="@drawable/ic_play"
            app:minImage="@drawable/ic_play"
            app:progressColor="@color/colorGreen"
            app:textEnabled="false"
            app:touchDisabled="false" />

    </android.support.v7.widget.CardView>


    <TextView
        android:id="@+id/tvOneScore"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="Streak"
        android:textColor="@color/colorBlue"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/firstCardView"
        app:layout_constraintStart_toStartOf="@+id/firstCardView"
        app:layout_constraintTop_toBottomOf="@+id/firstCardView" />


    <TextView
        android:id="@+id/tvVs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="V/S"
        android:textColor="@color/colorBlue"
        android:textSize="40sp"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <android.support.v7.widget.CardView
        android:id="@+id/secondCardView"
        android:layout_width="60dp"
        android:layout_height="250dp"
        android:layout_marginStart="10dp"
        app:cardElevation="5dp"
        app:layout_constraintBottom_toBottomOf="@+id/tvVs"
        app:layout_constraintStart_toEndOf="@+id/tvVs"
        app:layout_constraintTop_toTopOf="@+id/tvVs">

        <abak.tr.com.boxedverticalseekbar.BoxedVertical
            android:id="@+id/secondSeekBar"
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:layout_gravity="center"
            ads:points="50"
            app:backgroundColor="@android:color/white"
            app:defaultImage="@drawable/ic_play"
            app:imageEnabled="true"
            app:max="100"
            app:maxImage="@drawable/ic_play"
            app:minImage="@drawable/ic_play"
            app:progressColor="@color/colorGreen"
            app:textEnabled="false"
            app:touchDisabled="false" />

    </android.support.v7.widget.CardView>

    <TextView
        android:id="@+id/tvTwoScore"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:gravity="center"
        android:text="Steps"
        android:textColor="@color/colorBlue"
        android:textSize="12sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="@+id/secondCardView"
        app:layout_constraintStart_toStartOf="@+id/secondCardView"
        app:layout_constraintTop_toBottomOf="@+id/secondCardView" />


    <TextView
        android:id="@+id/tvWin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@android:color/white"
        android:gravity="center"
        android:padding="10dp"
        android:textColor="@color/colorBlue"
        android:text="@string/str_win"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tvTwoScore" />

    <ImageView
        android:id="@+id/tvPlayGame"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_marginBottom="40dp"
        android:scaleType="fitXY"
        android:src="@drawable/ic_play"
        app:backgroundTint="#0b961e"
        app:layout_constraintBottom_toBottomOf="@id/bottomLayout"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tvWin" />


    <RelativeLayout
        android:id="@+id/bottomLayout"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <com.leinardi.android.speeddial.SpeedDialOverlayLayout
            android:id="@+id/speedDialOverlayLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#81000000"
            app:clickable_overlay="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

        <com.leinardi.android.speeddial.SpeedDialOverlayLayout
            android:id="@+id/speedDialOverlayLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#81000000"
            app:clickable_overlay="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />


        <com.leinardi.android.speeddial.SpeedDialView
            android:id="@+id/speedDial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/mAdView"
            android:layout_alignParentEnd="true"
            ads:layout_constraintEnd_toEndOf="parent"
            ads:layout_constraintHorizontal_bias="0.5"
            app:backgroundTint="@android:color/white"
            app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior"
            app:sdExpansionMode="top"
            app:sdMainFabAnimationRotateAngle="90"
            app:sdMainFabClosedBackgroundColor="@android:color/white"
            app:sdMainFabClosedSrc="@drawable/ic_settings"
            app:sdMainFabOpenedBackgroundColor="#4e4c4c"
            app:sdMainFabOpenedSrc="@drawable/ic_close"
            app:sdUseReverseAnimationOnClose="true" />

        <com.leinardi.android.speeddial.SpeedDialView
            android:id="@+id/langSpeedDial"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/mAdView"
            android:layout_alignParentStart="true"
            android:layout_marginStart="8dp"
            ads:layout_constraintHorizontal_bias="0.5"
            ads:layout_constraintStart_toStartOf="parent"
            app:layout_behavior="@string/speeddial_scrolling_view_snackbar_behavior"
            app:sdExpansionMode="top"
            app:sdMainFabAnimationRotateAngle="90"
            app:sdMainFabClosedBackgroundColor="@android:color/white"
            app:sdMainFabClosedSrc="@drawable/ic_language"
            app:sdMainFabOpenedBackgroundColor="#4e4c4c"
            app:sdMainFabOpenedSrc="@drawable/ic_close"
            app:sdUseReverseAnimationOnClose="true" />


        <com.google.android.gms.ads.AdView
            android:id="@+id/mAdView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            ads:adSize="BANNER"
            ads:adUnitId="@string/bannerID" />

    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

我在使用上面的代码时遇到以下问题

发布截图

-issue in large screen

-issue in small screen

我检查了一些 SO Post

请检查下面的屏幕我的预期输出

my expected output

任何人都可以使用 ConstraintLayout 布局帮助我创建此布局

如果需要更多信息,请告诉我。提前致谢。您的努力将不胜感激。

您可以通过使用 Guidelines 来支持不同的屏幕尺寸,Guidelines 基本上是从 android.view.View 继承并专门为约束创建的组件 考虑到这一点,您将能够使用它的属性百分比 这是组件示例的片段

<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline8"
app:layout_constraintGuide_percent="0.9"
android:orientation="horizontal"/>

这意味着在屏幕的 90% 处将有一个指南,您可以将您的组件设置为具有该指南的从上到下约束而不是按钮,因此它没有任何奇怪的行为并且它是 bottomToBottom 约束如果你想让它固定在屏幕的底部,就成为父级......等等

这是一个例子: 你会发现 imageview 现在将其底部限制在屏幕的 90%,无论你对 textview 做了什么,同样对于 textview,因为它的顶部是 90%,不管你在 imageview 中做什么,并且也固定到父级它是 app:layout_constraintBottom_toBottomOf="parent"

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
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/home_bg">

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline8"
    app:layout_constraintGuide_percent="0.9"
    android:orientation="horizontal"/>

<ImageView
    android:id="@+id/topImage"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_marginTop="20dp"
    android:contentDescription="@string/app_name"
    android:src="@drawable/splash_heading"
    app:layout_constraintStart_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/guideline8"/>

<TextView
    android:id="@+id/tvAllOverResult"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:padding="10dp"
    android:text="@string/allover_result"
    android:textColor="@color/colorBlue"
    android:textSize="20sp"
    android:textStyle="bold"
    ads:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/guideline8"
    app:layout_constraintBottom_toBottomOf="parent"/>


</android.support.constraint.ConstraintLayout>

我真的很喜欢这个问题,因为你真的很努力,当你很清楚你的意图时,它很容易帮助你。

现在 - 当您使用 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:id="@+id/frameLayout2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Fragments.MenusDesign.ExpandableCategoriesMenu.ExpandableCategoriesMenu">

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Dummy text"
        app:layout_constraintBottom_toTopOf="@+id/guideline2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="V/s"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toStartOf="@+id/button7"
        app:layout_constraintStart_toEndOf="@+id/button5"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="streak"
        app:layout_constraintBottom_toTopOf="@+id/textView8"
        app:layout_constraintEnd_toStartOf="@+id/textView10"
        app:layout_constraintStart_toStartOf="@+id/guideline5"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="steps"
        app:layout_constraintBottom_toTopOf="@+id/textView9"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toEndOf="@+id/textView10"
        app:layout_constraintTop_toTopOf="@+id/guideline2" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="@+id/button3"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/button3" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="setings"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/guideline4"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <Button
        android:id="@+id/button4"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="16dp"
        android:text="Ad"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/guideline4"
        app:layout_constraintStart_toStartOf="@+id/guideline5" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.2" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.6" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="@+id/button5"
        app:layout_constraintStart_toStartOf="@+id/button5" />

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toTopOf="@+id/guideline3"
        app:layout_constraintEnd_toEndOf="@+id/button7"
        app:layout_constraintStart_toStartOf="@+id/button7" />

    <TextView
        android:id="@+id/textView10"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:text="Dummy text"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/guideline3" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.8" />

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.2" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Play"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintEnd_toStartOf="@+id/button3"
        app:layout_constraintStart_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/textView10" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toTopOf="@+id/textView4"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:src="@tools:sample/avatars[5]" />
</androidx.constraintlayout.widget.ConstraintLayout>

所以...我在这里到底做了什么?

基本上 - 我在屏幕的某个百分比处使用了指南(例如 1 个约束在 20%,另一个在 30% ).在那之后,我将我的观点限制在这些指导方针上,通过这样做我可以实现 在一些礼物中以我想要的相对于屏幕的大小查看。