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

时间:2019-03-09 09:24:06

标签: android android-layout android-constraintlayout constraint-layout-chains

我正在使用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>

使用上述代码,我面临以下问题

  • 在小屏幕中,imageview藏在addview后面
  • 当我将addview隐藏在底部SpeedDialView并没有进行调整
  • 如何约束SpeedDialView,使其在我隐藏addview时与可见的addview一样起作用

问题截图

-issue in large screen

-issue in small screen

我已经检查了一些SO Post

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

my expected output

有人可以帮我使用ConstraintLayout布局创建此布局吗?

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

2 个答案:

答案 0 :(得分:1)

您可以通过使用“指南”来支持不同的屏幕尺寸,该指南基本上是从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中做什么,并且固定于父级的应用是: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>

答案 1 :(得分:1)

我真的很喜欢这个问题,因为您确实表现出了自己的努力,当您对自己的意图很清楚时,它很容易为您提供帮助。

现在-,当您使用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%)。在那之后,我将我的观点限制在那些准则中,这样我就可以实现 以相对于屏幕的当前尺寸显示我想要的尺寸。