Android约束布局:在2条准则之间将视图居中

时间:2018-08-06 14:29:14

标签: android android-constraintlayout

我将屏幕分成4列,它们是屏幕宽度的25%,然后将TextView放置在准则1和准则2之间。问题是,它们似乎不在准则之间居中。

我尝试了LeftOf,StartOf,EndOf,gravity和TextAlign。自从我做了约束以来已经有一段时间了

    <?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="wrap_content"
    android:background="@drawable/title_bar"
    app:layout_constraintBottom_toBottomOf="parent">

  <android.support.constraint.Guideline
      android:id="@+id/guideline2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.5" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.25" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.75" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeInternetLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      android:text="@string/home_internet_label"
      app:layout_constraintRight_toRightOf="@id/guideline1"
      app:layout_constraintLeft_toLeftOf="parent" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeInternetStatus"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_height="wrap_content"
      android:layout_width="0dp"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="@id/guideline1"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeInternetLabel"
      android:text="@string/home_internet_status" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeServerLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline1"
      app:layout_constraintRight_toRightOf="@id/guideline2"
      android:text="@string/home_server_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeServerStatus"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline1"
      app:layout_constraintRight_toRightOf="@id/guideline2"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeServerLabel"
      android:text="@string/home_server_status" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeLastSyncLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline2"
      app:layout_constraintRight_toRightOf="@id/guideline3"
      android:text="@string/home_last_sync_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeLastSyncDateTime"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline2"
      app:layout_constraintRight_toRightOf="@id/guideline3"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeLastSyncLabel"
      android:text="@string/home_last_sync_value" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeViewLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline3"
      app:layout_constraintRight_toRightOf="parent"
      android:text="@string/home_view_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeViewValue"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="0dp"
      android:layout_height="wrap_content"
      app:layout_constraintLeft_toLeftOf="@id/guideline3"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeViewLabel"
      android:text="@string/home_view_value" />

</android.support.constraint.ConstraintLayout>

**更新2 **

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"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/title_bar"
    app:layout_constraintBottom_toBottomOf="parent">

  <android.support.constraint.Guideline
      android:id="@+id/guideline2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.5" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.25" />

  <android.support.constraint.Guideline
      android:id="@+id/guideline3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintGuide_percent="0.75" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeInternetLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      android:text="@string/home_internet_label"
      app:layout_constraintRight_toRightOf="@id/guideline1"
      app:layout_constraintLeft_toLeftOf="parent" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeInternetStatus"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_height="wrap_content"
      android:layout_width="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="@id/guideline1"
      app:layout_constraintBottom_toBottomOf="@+id/textViewHomeInternetLabel"
      android:text="@string/home_internet_status" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeServerLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline1"
      app:layout_constraintRight_toRightOf="@id/guideline2"
      android:text="@string/home_server_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeServerStatus"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline1"
      app:layout_constraintRight_toRightOf="@id/guideline2"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeServerLabel"
      android:text="@string/home_server_status" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeLastSyncLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline2"
      app:layout_constraintRight_toRightOf="@id/guideline3"
      android:text="@string/home_last_sync_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeLastSyncDateTime"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline2"
      app:layout_constraintRight_toRightOf="@id/guideline3"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeLastSyncLabel"
      android:text="@string/home_last_sync_value" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeViewLabel"
      style="@style/Theme.Reach.HomeStatusPhone.Label"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline3"
      app:layout_constraintRight_toRightOf="parent"
      android:text="@string/home_view_label" />

  <uk.co.reach.gc.widget.CustomFontTextView
      android:id="@+id/textViewHomeViewValue"
      style="@style/Theme.Reach.HomeStatusPhone"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:gravity="center"
      app:layout_constraintLeft_toLeftOf="@id/guideline3"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/textViewHomeViewLabel"
      android:text="@string/home_view_value" />

</android.support.constraint.ConstraintLayout>

3 个答案:

答案 0 :(得分:1)

我刚刚运行了您的代码,似乎确实缺少了重力属性(android:gravity="center")。您的指南似乎很好,因为所有内容都分成25%的部分。

如果重力不起作用,请检查您的样式以确认是否已设置,可以添加app:而不是android:,反之亦然。尝试在XML中的TextView上添加重力,然后删除样式,然后从那里开始。

答案 1 :(得分:0)

我怀疑如果将此属性添加到TextViews中,问题将会消失:

android:gravity="center_horizontal"

或者,您可以将宽度更改为:

android:layout_width="wrap_content"

目前情况如此,您正在将android:layout_width="0dp"与左,右约束结合在一起。这将使视图的拉伸宽度恰好等于这两个准则之间的间隔...但是,如果文本长度短于视图宽度,则TextView在默认情况下会左对齐其文本。

答案 2 :(得分:0)

据我了解,guideline1位于guideline2之前,因此以版式的这一部分为例:

<uk.co.reach.gc.widget.CustomFontTextView
  android:id="@+id/textViewHomeServerLabel"
  style="@style/Theme.Reach.HomeStatusPhone.Label"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  app:layout_constraintLeft_toLeftOf="@id/guideline1"
  app:layout_constraintRight_toRightOf="@id/guideline2"
  android:text="@string/home_server_label" />

应为:

<uk.co.reach.gc.widget.CustomFontTextView
  android:id="@+id/textViewHomeServerLabel"
  style="@style/Theme.Reach.HomeStatusPhone.Label"
  android:layout_width="0dp"
  android:layout_height="wrap_content"
  app:layout_constraintLeft_toRightOf="@id/guideline1"
  app:layout_constraintRight_toLeftOf="@id/guideline2"
  android:text="@string/home_server_label" />

最后,您的constraintLeft和constraintRight错误。尝试对其他视图也进行这些更改。