【问题标题】:Android : Constraint Layout Set Constraint top programmatically?Android:约束布局以编程方式设置约束顶部?
【发布时间】:2018-05-30 05:17:30
【问题描述】:

我有 3 个视图:A、B、C。 (A 和 B 的高度相等)一开始 B 的可见性消失了,C 的顶部约束是 A 的底部,因此 C 出现在 A 下方。一段时间后,我将 A 的可见性更改为消失,将 B 的可见性更改为可见。发生的情况是 C 被拖到顶部,因为 A 的可见性消失了。我想要做的是将 C 的顶部约束设置为 B 的底部。我该怎么做?我需要以编程方式进行。

这是我现在所在的位置 ->

<?xml version="1.0" encoding="utf-8"?>

 //A
<LinearLayout

    android:onClick="clickedOnRecordLayout"
    android:layout_marginTop="@dimen/record_layout_top_margin"
    android:id="@+id/record_button_layout"
    android:gravity="center"
    android:elevation="@dimen/elevation_of_record_button"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/red_circle_drawable"
    android:layout_width="@dimen/radius_of_record_button"
    android:layout_height="@dimen/radius_of_record_button">

    <ImageView
        android:id="@+id/record_image"
        android:src="@drawable/ic_microphone"
        android:layout_width="@dimen/record_image_dimen"
        android:layout_height="@dimen/record_image_dimen" />
</LinearLayout>

 //B - initially its visibility is gone
<LinearLayout
    android:onClick="clickedOnStartedRecordingLayout"
    android:visibility="gone"
    android:layout_marginTop="@dimen/record_layout_top_margin"
    android:id="@+id/started_button_layout"
    android:gravity="center"
    android:elevation="@dimen/elevation_of_record_button"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:background="@drawable/red_circle_drawable"
    android:layout_width="@dimen/radius_of_record_button"
    android:layout_height="@dimen/radius_of_record_button">

    <ImageView
        android:id="@+id/stop_image"
        android:src="@drawable/ic_stop_recording"
        android:layout_width="@dimen/record_image_dimen"
        android:layout_height="@dimen/record_image_dimen" />
</LinearLayout>


//C
<TextView
    android:layout_marginTop="@dimen/tap_text_top_margin"
    app:layout_constraintTop_toBottomOf="@id/record_button_layout"
    android:id="@+id/tap_on_microphone_tv"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:text="@string/tap_to_start_message"
    android:textAlignment="center"
    android:textColor="@android:color/black"
    android:textStyle="bold"
    app:layout_constraintBottom_toTopOf="@id/chronometer"
    app:layout_constraintHorizontal_bias="0.503"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent" />

【问题讨论】:

标签: java android android-layout kotlin android-constraintlayout


【解决方案1】:

欢迎您使用 ConstraintSet,但如果您想保持简单,我想提请您注意

ConstraintLayout 对标记为 View.GONE 的小部件进行了特定处理。

像往常一样,GONE 小部件不会显示并且不是一部分 布局本身(即它们的实际尺寸不会改变 如果标记为 GONE)。

但在布局计算方面,GONE 小部件仍然是一部分 其中,有一个重要的区别:

对于布局通道,它们的尺寸将被视为零 (基本上,他们会解决一个点)如果他们有约束 对于其他小部件,它们仍将受到尊重,但任何边距都将是 好像等于零 看看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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">

    <EditText     // View A
        android:id="@+id/email"
        android:hint="email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textEmailAddress"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText // View B
        android:id="@+id/password"
        android:hint="password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:visibility="gone"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/email" />
    <EditText   //View C
        android:id="@+id/otp"
        android:hint="otp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:inputType="textPassword"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/password" />

    <Button
        android:id="@+id/login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Login"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>

这个布局看起来像

当您设置 email 字段时,即视图 A 已消失

当您将密码设置为 GONE 时,即 View B

这就是约束布局的魔力。

【讨论】:

    猜你喜欢
    • 2015-12-26
    • 2021-11-22
    • 2017-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    • 2018-04-08
    相关资源
    最近更新 更多