如何使用约束布局将imageView在第三行居中

时间:2018-11-19 07:39:08

标签: android centering android-constraintlayout

2x2 plus 1 un-centered tile

我有5个正方形的瓷砖,所有这些瓷砖都必须具有相同的尺寸,但必须具有柔韧性,以便可以增长和缩小以适应不同的显示器尺寸。每个图块下方都有文字。我希望最后一行水平居中,并希望在可能的情况下仅使用xml。我也有一个工具栏和一个导航栏。这是我的xml文件:

<?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/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" android:background="@drawable/sunrise" tools:layout_editor_absoluteY="25dp">

<android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/toolBarColor"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navigationIcon="@drawable/refresh2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Now - The Technology"
        android:textColor="@color/appTitleColor"
        android:textSize="18sp"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"/>
</android.support.v7.widget.Toolbar>

<ImageView
        android:id="@+id/ivPlay"
        app:layout_constraintDimensionRatio="1:1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/play"
        android:scaleType="fitXY" android:adjustViewBounds="true"
        android:contentDescription="@string/con_des_play"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
        android:layout_marginTop="16dp"
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart="16dp"
        app:layout_constraintEnd_toStartOf="@+id/ivReminder"
        android:layout_marginEnd="16dp"
/>

<TextView android:id="@+id/tvPlay"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="@+id/ivPlay"
          app:layout_constraintEnd_toEndOf="@+id/ivPlay"
          app:layout_constraintTop_toBottomOf="@+id/ivPlay"
          android:layout_marginBottom="2dp"
          android:text="@string/play_10_minutes"
          android:textColor="@color/iconTextColor"
          android:textSize="16sp"
/>

<ImageView
        android:id="@+id/ivReminder"
        app:layout_constraintDimensionRatio="1:1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/reminder"
        android:scaleType="fitXY" android:adjustViewBounds="true"
        android:contentDescription="@string/con_des_play"
        app:layout_constraintTop_toBottomOf="@+id/my_toolbar"
        android:layout_marginTop="16dp"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginEnd="16dp"
        app:layout_constraintStart_toEndOf="@+id/ivPlay"
        android:layout_marginStart="16dp"
/>

<TextView android:id="@+id/tvReminder"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="@+id/ivReminder"
          app:layout_constraintEnd_toEndOf="@+id/ivReminder"
          app:layout_constraintTop_toBottomOf="@+id/ivReminder"
          android:layout_marginBottom="2dp"
          android:text="@string/now_reminder"
          android:textColor="@color/iconTextColor"
          android:textSize="16sp"
/>

<ImageView
        android:id="@+id/ivGuide"
        app:layout_constraintDimensionRatio="1:1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/guide"
        android:scaleType="fitXY" android:adjustViewBounds="true"
        android:contentDescription="@string/con_des_play"
        app:layout_constraintTop_toBottomOf="@+id/tvPlay"
        android:layout_marginTop="20dp"
        app:layout_constraintStart_toStartOf="@+id/ivPlay"
        app:layout_constraintEnd_toEndOf="@+id/ivPlay"
        app:layout_constraintHorizontal_bias="1.0"/>

<TextView android:id="@+id/tvGuide"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="@+id/ivGuide"
          app:layout_constraintEnd_toEndOf="@+id/ivGuide"
          app:layout_constraintTop_toBottomOf="@+id/ivGuide"
          android:layout_marginBottom="2dp"
          android:text="@string/guide"
          android:textColor="@color/iconTextColor"
          android:textSize="16sp"
/>

<ImageView
        android:id="@+id/ivUpgrade"
        app:layout_constraintDimensionRatio="1:1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/upgrade"
        android:scaleType="fitXY" android:adjustViewBounds="true"
        android:contentDescription="@string/con_des_play"
        app:layout_constraintTop_toTopOf="@+id/ivGuide"
        app:layout_constraintStart_toStartOf="@+id/ivReminder"
        app:layout_constraintEnd_toEndOf="@+id/ivReminder"

/>

<TextView android:id="@+id/tvUpgrade"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="@+id/ivUpgrade"
          app:layout_constraintEnd_toEndOf="@+id/ivUpgrade"
          app:layout_constraintTop_toBottomOf="@+id/ivUpgrade"
          android:layout_marginTop="2dp"
          android:text="@string/upgrade"
          android:textColor="@color/iconTextColor"
          android:textSize="16sp"
/>

<ImageView
        android:id="@+id/ivLearn"
        app:layout_constraintDimensionRatio="1:1"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:srcCompat="@drawable/learn"
        android:scaleType="fitXY" android:adjustViewBounds="true"
        android:contentDescription="@string/con_des_play"
        app:layout_constraintTop_toBottomOf="@+id/tvGuide"
        app:layout_constraintStart_toStartOf="@+id/ivGuide"
        app:layout_constraintEnd_toEndOf="@+id/ivGuide"
        app:layout_constraintBottom_toTopOf="@+id/tvLearn"
        android:layout_marginTop="16dp"
        android:foregroundGravity="center_horizontal"
        app:layout_constraintHorizontal_bias="0.5"/>

<TextView android:id="@+id/tvLearn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="@+id/ivLearn"
          app:layout_constraintEnd_toEndOf="@+id/ivLearn"
          app:layout_constraintTop_toBottomOf="@+id/ivLearn"
          app:layout_constraintBottom_toTopOf="@+id/navigation"
          android:text="@string/learn"
          android:textColor="@color/iconTextColor"
          android:textSize="16sp"
          android:layout_marginTop="2dp"
          android:layout_marginBottom="16dp"
/>

<android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="#D3D3D3"
        app:menu="@menu/navigation" app:itemTextColor="@color/navAccent" style="@style/AppTheme"
        android:visibility="visible" app:itemIconTint="@color/navAccent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="0dp"/>
</android.support.constraint.ConstraintLayout>

谢谢。

2 个答案:

答案 0 :(得分:1)

尝试更改android:id="@+id/ivLearn"的属性

来自

app:layout_constraintStart_toStartOf="@+id/ivGuide"
app:layout_constraintEnd_toEndOf="@+id/ivGuide"

app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

答案 1 :(得分:0)

我还没有尝试过,但是回收站视图似乎是android团队最好和最灵活的答案。我使用了百分比形式的垂直指南,我需要在不同的屏幕尺寸上进行测试,这似乎至少可以在一个屏幕尺寸上使用。我还必须将Java代码转换为我正在学习的Kotlin。

谢谢。