使用约束布局动态设置视图宽度

时间:2018-09-10 13:10:51

标签: android android-constraintlayout

我是ConstraintLayout的新手,下面是我的xml文件,我正在尝试动态实现Textview的宽度。如果我的@ + id / btn1可见性消失了,则TextView(@ + id / tv1)应该扩展为全宽。请提出我应该采用哪种方法来解决此问题。

 <?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="match_parent"
    android:layout_margin="@dimen/default_gap"
    app:layout_anchorGravity="right">


    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="First thing first, if you have a min SDK lower than 17, you can now shift to using only start and end attribute and don’t repeat yourself for right and left"
        app:layout_constraintEnd_toEndOf="@+id/guideline_center"
        app:layout_constraintStart_toStartOf="parent" />

    <Button
        android:id="@+id/tv_cur_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RS"
        app:layout_constraintTop_toBottomOf="@+id/tv1" />

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


    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline_center" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Payment Mode"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline_center"
        app:layout_constraintTop_toBottomOf="@+id/btn1" />


</android.support.constraint.ConstraintLayout>

下面是屏幕截图,enter image description here现在的样子

所需的用户界面enter image description here

1 个答案:

答案 0 :(得分:0)

  

删除textview endconstraint并连接到按钮,并在按钮和textview之间建立水平链,然后当按钮视图消失时,textview将使用fullwdith。

尝试一下

 <android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:layout_marginBottom="20dp"
    android:layout_marginEnd="21dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="21dp"
    android:layout_marginStart="20dp"
    android:layout_marginTop="20dp"
    app:layout_anchorGravity="right">


    <TextView
        android:id="@+id/tv1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:text="First thing first, if you have a min SDK lower than 17, you can now shift to using only start and end attribute and don’t repeat yourself for right and left"
        app:layout_constraintEnd_toStartOf="@+id/btn1"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/tv_cur_type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RS"
        app:layout_constraintTop_toBottomOf="@+id/tv1" />

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


    <Button
        android:id="@+id/btn1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:visibility="visible"
        app:layout_constraintBottom_toTopOf="@+id/tv2"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/tv1"
        app:layout_constraintTop_toTopOf="parent"
        />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Payment Mode"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/guideline_center"
        app:layout_constraintTop_toBottomOf="@+id/btn1" />


</android.support.constraint.ConstraintLayout>