如何使用约束布局在屏幕边缘附近放置视图?

时间:2017-12-08 12:34:50

标签: android android-layout android-constraintlayout android-gridlayout

我正在尝试将GridLayout更改为ConstraintLayout。

我的网格看起来像这样:

<android.support.v7.widget.GridLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_asset"

    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"

    app:layout_gravity="center_horizontal"
    app:layout_column="0"
    app:layout_row="0"

    app:hideText="true"
    app:skipStartColor="true"/>

<TextView
    android:id="@+id/progress_text_stage_asset"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_small"

    app:layout_gravity="center_horizontal"
    app:layout_column="0"
    app:layout_row="1"

    style="@style/AppWidget.Text.Emphasized"
    android:gravity="center"
    android:textColor="@color/circular_progress_middle_color"
    android:text="text text text" />

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_sync"

    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"

    app:layout_gravity="center_horizontal"
    app:layout_column="2"
    app:layout_row="0"

    app:hideText="true"
    app:skipStartColor="true"/>

<TextView
    android:id="@+id/progress_text_stage_sync"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_small"

    app:layout_gravity="center_horizontal"
    app:layout_column="2"
    app:layout_row="1"

    style="@style/AppWidget.Text.Emphasized"
    android:gravity="center"
    android:textColor="@color/circular_progress_middle_color"
    android:text="text text text" />

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_completed"

    android:layout_width="@dimen/circular_color_progress_size_medium"
    android:layout_height="@dimen/circular_color_progress_size_medium"

    app:layout_gravity="center_horizontal"
    app:layout_column="0"
    app:layout_row="3"

    app:hideText="true"
    app:skipStartColor="true"/>

<TextView
    android:id="@+id/progress_text_stage_completed"

    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/margin_small"

    app:layout_gravity="center_horizontal"
    app:layout_column="0"
    app:layout_row="4"

    style="@style/AppWidget.Text.Emphasized"
    android:gravity="center"
    android:textColor="@color/circular_progress_middle_color"
    android:text="@string/msg_in_store_commissioning_stage_completed" />

<View
    android:layout_width="0dp"
    android:layout_height="0dp"

    app:layout_column="1"
    app:layout_row="2"
    app:layout_columnWeight="1"
    app:layout_rowWeight="1"/>

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_params"

    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"

    app:layout_gravity="center"
    app:layout_column="2"
    app:layout_row="3"

    app:hideText="true"
    app:skipStartColor="true"/>

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

它产生如下输出:

enter image description here

我是ConstraintLayout的新手。使用约束布局是否有一种简单的方法可以实现相同的行为?我想它会更有效率和干净。

使用RelativeLayout只是对齐左,右,底等。 使用LinearLayout只是在这些循环视图之间放置一些空视图,并为它们添加特定权重。

但是如何使用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"
android:layout_width="match_parent"
android:layout_height="match_parent">


<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_asset"
    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"
    android:layout_marginStart="16dp"
    app:hideText="true"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:skipStartColor="true" />

<TextView
    android:id="@+id/progress_text_stage_asset"

    style="@style/AppWidget.Text.Emphasized"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="12dp"

    android:gravity="center"
    android:text="text text text"
    android:textColor="@color/circular_progress_middle_color"
    app:layout_column="0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progress_stage_asset"
    app:layout_gravity="center_horizontal"
    app:layout_row="1" />

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_sync"
    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"
    android:layout_marginEnd="16dp"

    app:hideText="true"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:skipStartColor="true" />

<TextView
    android:id="@+id/progress_text_stage_sync"
    style="@style/AppWidget.Text.Emphasized"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginEnd="8dp"
    android:layout_marginTop="@dimen/margin_small"
    android:gravity="center"
    android:text="text text text"
    android:textColor="@color/circular_progress_middle_color"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progress_stage_sync"
    app:layout_gravity="center_horizontal" />

<com.app.widget.CircularColorProgressView
    android:id="@+id/progress_stage_completed"

    android:layout_width="@dimen/circular_color_progress_size_medium"
    android:layout_height="@dimen/circular_color_progress_size_medium"

    android:layout_marginBottom="48dp"


    app:hideText="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_gravity="center_horizontal"
    app:skipStartColor="true" />

<TextView
    android:id="@+id/progress_text_stage_completed"

    style="@style/AppWidget.Text.Emphasized"
    android:layout_width="wrap_content"

    android:layout_height="wrap_content"

    android:layout_marginStart="3dp"
    android:layout_marginTop="@dimen/margin_small"
    android:gravity="center"
    android:text="@string/msg_in_store_commissioning_stage_completed"
    android:textColor="@color/circular_progress_middle_color"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/progress_stage_completed"
    app:layout_gravity="center_horizontal" />

<com.app.widget.CircularColorProgressView
    android:layout_width="@dimen/circular_color_progress_size_small"
    android:layout_height="@dimen/circular_color_progress_size_small"
    android:layout_marginBottom="80dp"
    android:layout_marginEnd="16dp"
    app:hideText="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:skipStartColor="true" />

它与Grid非常类似: enter image description here

2 个答案:

答案 0 :(得分:0)

如果你在android studio中使用设计编辑器来实现这一点会很容易。

在视图中添加android.support.constraint.ConstraintLayout作为父级

<强>&GT;要对齐左上角的视图

  app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"

在右上方对齐视图

 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintTop_toTopOf="parent"

对齐左下角的视图

 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintStart_toStartOf="parent"

在右下方对齐视图

  app:layout_constraintBottom_toBottomOf="parent"
  app:layout_constraintEnd_toEndOf="parent"

将视图与另一个视图的下方对齐(在您的情况下为TextView)

 app:layout_constraintTop_toBottomOf="@+id/viewId"

答案 1 :(得分:0)

试试这个: enter image description here

    <?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/gridLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ProgressBar
        android:id="@+id/progress_stage_asset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
         />

    <TextView
        android:id="@+id/progress_text_stage_asset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text text text"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="@+id/progress_stage_asset"
        app:layout_constraintRight_toRightOf="@+id/progress_stage_asset"
        app:layout_constraintTop_toBottomOf="@+id/progress_stage_asset" />

    <ProgressBar
        android:id="@+id/progress_stage_sync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
         />

    <TextView
        android:id="@+id/progress_text_stage_sync"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="text text text"
        app:layout_constraintLeft_toLeftOf="@id/progress_stage_sync"
        app:layout_constraintLeft_toRightOf="@+id/progress_stage_sync"
        app:layout_constraintTop_toBottomOf="@+id/progress_stage_sync"/>

    <ProgressBar
        android:id="@+id/progress_stage_completed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/progress_text_stage_completed"
        />

    <TextView
        android:id="@+id/progress_text_stage_completed"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintLeft_toLeftOf="@+id/progress_stage_completed"
        app:layout_constraintRight_toRightOf="@+id/progress_stage_completed"
        app:layout_constraintBottom_toBottomOf="parent"/>

    <View
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <ProgressBar
        android:id="@+id/progress_stage_params"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="20dp"
       />

</android.support.constraint.ConstraintLayout>