Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在ConstraintLayout中自动调整限制_Android_Android Layout_Android Constraintlayout - Fatal编程技术网

Android 在ConstraintLayout中自动调整限制

Android 在ConstraintLayout中自动调整限制,android,android-layout,android-constraintlayout,Android,Android Layout,Android Constraintlayout,我有一个表单作为ConstraintLayout,如下所示: <TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="24dp" android:stretchColumns="1"> <TableRow> <TextView android:gravity="end"

我有一个表单作为
ConstraintLayout
,如下所示:

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:stretchColumns="1">
    <TableRow>
        <TextView android:gravity="end"
            android:text="Full Name"/>
        <EditText/>
    </TableRow>
    <TableRow>
        <TextView android:gravity="end"
            android:text="Password"/>
        <EditText/>
    </TableRow>
</TableLayout>

“全名”和“密码”在其右边缘对齐,“全名”提供了来自
ConstraintLayout
的边距:

到目前为止,一切都很好,但我想知道如何正确处理需要以编程方式更改这些文本的情况。如果“全名”变大或变小,“密码”将保持对齐:

但是,如果“密码”更改其大小,它将退出屏幕:

我尝试使用
屏障
获取“全名”和“密码”的左边缘,但我无法使其工作,我认为
屏障
只能与其他元素相关,而不能将屏障的元素与边缘对齐(如果我错了,请纠正我)

这个案例对我来说尤其重要,因为它避免了为语言制作几种不同的布局,其中“全名”和“密码”的翻译可能会改变它们的大小,并使文本脱离屏幕


那么,如果“全名”和“密码”的文本发生了变化,并且如图所示,
EditText
s扩展到其右边缘,我如何避免它们从屏幕上消失呢?

TableLayout
ConstraintLayout
在您的情况下,布局可能是这样的:

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="24dp"
    android:stretchColumns="1">
    <TableRow>
        <TextView android:gravity="end"
            android:text="Full Name"/>
        <EditText/>
    </TableRow>
    <TableRow>
        <TextView android:gravity="end"
            android:text="Password"/>
        <EditText/>
    </TableRow>
</TableLayout>


当您更改两个
TextView
中的一个时,它们将再次自动对齐。

这是对
ConstraintLayout
的一个有趣的测试。以下布局适用于版本1.1.2和1.1.3,但未在其他版本中测试

关键是将全名和密码字段的结尾相互约束,并将它们的开头约束到父字段的开头。将水平偏差设置为
1.0
,使两个字段右对齐

我想知道循环引用(end)是否会很高兴地解决,它确实解决了。我现在想知道,这个解决方案是否恰好有效,但在将来的版本中可能会中断

活动\u main.xml

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="Full Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@+id/password"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="Password is longer"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@id/fullName"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fullName" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/fullName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/fullName"
        app:layout_constraintTop_toTopOf="@id/fullName" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/password"
        app:layout_constraintTop_toTopOf="@id/password" />

</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/fullName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="50dp"
        android:text="Full Name"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toEndOf="@+id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="24dp"
        android:text="Password is longer"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintEnd_toStartOf="@id/barrier"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/fullName" />

    <View
        android:id="@+id/view"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/fullName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toTopOf="@id/fullName" />

    <View
        android:id="@+id/view2"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginStart="16dp"
        android:layout_marginEnd="16dp"
        android:background="@android:color/holo_blue_light"
        app:layout_constraintBottom_toBottomOf="@id/password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@id/barrier"
        app:layout_constraintTop_toTopOf="@id/password" />

    <androidx.constraintlayout.widget.Barrier
        android:id="@+id/barrier"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:barrierDirection="right"
        app:constraint_referenced_ids="fullName,password" />

</androidx.constraintlayout.widget.ConstraintLayout>

有趣的是,我将保持这个问题的开放性,只是为了学习
constraintlayout
的新技巧,以防有人使用屏障查看示例。您可以将屏障设置为最大标签的右侧,然后将编辑文本与屏障对齐。。。我的问题是左边应该还有可能。我试着举一个例子。看起来@Cheticamp比我快了一步。这个答案是一个很好的解决方案。它是可行的,但这只可能在代码上实现,Android Studio阻止循环引用有趣,所以它与我所做的+循环引用相反。我试图在可视化编辑器中创建一个循环引用,但它阻止了我,唯一可行的方法是在代码中强制引用,因此它可能是“谷歌不想要的”@mFeinstein补充道。我想我更喜欢这个。。。。循环参考号在哪里?它是否隐含在屏障中?是的,到屏障。我将很快测试它,看看Android Studio Designer是否允许我通过拖放来完成。如果是这样的话,我不认为它将来会破裂