【问题标题】:ConstraintLayout layout_constraintHorizontal_bias seems not workConstraintLayout layout_constraintHorizo​​ntal_bias 似乎不起作用
【发布时间】:2020-06-06 02:55:53
【问题描述】:

这是我的布局文件。

<?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:layout_width="match_parent"
    android:layout_height="match_parent">
    <EditText
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="parent" />
    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toTopOf="parent"
        app:layout_constraintHorizontal_bias="0.5" />
</android.support.constraint.ConstraintLayout>

下面的图片是预览视图。

我想将 TextView 移动到 center_horizo​​ntal 到父视图,但layout_constraintHorizontal_bias=0.5" 似乎不起作用。

谁有这个问题的想法?先谢谢了!

【问题讨论】:

  • 你的预期输出是什么
  • @Nilu 将 TextView 移动到水平居中
  • @Cyrus 你想只通过 ConstraintLayout 来实现吗?
  • 将 android:layout_gravity="center" 添加到文本视图中
  • @VishvaDave 是的,我正在学习如何使用 ConstraintLayout

标签: android android-layout android-constraintlayout


【解决方案1】:

试试这个

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


    <EditText
        android:id="@+id/editText2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

编辑

要使用 bias,您需要将其约束给任何父级。

【讨论】:

  • 感谢您的回答,这样可以。但为什么 'app:layout_constraintHorizo​​ntal_bias=0.5' 不起作用。
  • @Cyrus 我认为不需要layout_constraintHorizontal_bias 检查更新的答案
  • @Cyrus 你需要工作,因为你没有将它限制在任何父级到 textview
  • @Cyrus 很乐意帮助你
  • 注意:默认水平偏差为 0.5,因此您可以省略 app:layout_constraintHorizo​​ntal_bias="0.5"
【解决方案2】:

试试这个:

app:layout_constraintHorizontal_bias="0.5" 无法正常工作,因为 你没有把它限制在任何父母身上。 Read more about bias.

<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Hello"
        android:textColor="@android:color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/editText2"
        android:layout_width="150dp"
        android:layout_height="50dp"
        android:layout_marginBottom="16dp"
        android:ems="10"
        android:hint="please input test content"
        android:inputType="phone"
        android:textColorHint="@android:color/holo_red_light"
        android:textSize="13sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

【讨论】:

  • @Cyrus app:layout_constraintHorizontal_bias="0.5" 不起作用,因为您没有将其限制为任何父级。试试我的回答,希望对你有帮助。
  • 与@Nilesh 的响应相同
  • 解决方案只有一个答案..但这是每个人自己的写法
  • @Badyous 如您所见,nilesh 的答案也与 jesper 的答案相同...因为此解决方案只有一个答案...请删除不赞成票
【解决方案3】:

首先,如果我是你,我会看一些解释约束布局的指南,因为你似乎缺少一些关键元素。 ConstraintLayout

话虽如此,您的问题是您要求布局将 0.5 偏差限制为无。您的 textView 不受 app:layout_constraintBottom_toTopOf="parent" 以外的任何限制 顺便说一句,将文本视图的底部限制在父视图的顶部有点奇怪。

为了使偏差起作用,它需要知道要在哪些元素之间产生偏差。如果您只是希望它成为父级的中心,则将 textview 约束到父级,如下所示:

<TextView
    android:layout_width="80dp"
    android:layout_height="50dp"
    android:gravity="center"
    android:text="Hello"
    android:textColor="@android:color/black"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintHorizontal_bias="0.5" />

此时您也不需要偏差,但我将其保留,因为您可以使用它以其他百分比移动它。

【讨论】:

  • 我也在最后一行添加了。
【解决方案4】:

没有必要将偏差作为用于元素之间间距的偏差,因为它不需要从左、右下角和顶部约束到父级

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-04-14
    • 2016-11-29
    • 2016-02-01
    • 2020-09-23
    • 2010-12-05
    • 2011-06-14
    • 2015-01-10
    • 2016-02-24
    相关资源
    最近更新 更多