2017-08-02 148 views
0

我有一个TextView:约束布局改变约束编程

<TextView 
    android:id="@+id/textViewChat" 
    android:layout_width="0dp" 
    android:layout_height="wrap_content" 
    android:layout_marginEnd="8dp" 
    android:layout_marginLeft="8dp" 

    android:layout_marginRight="16dp" 
    android:layout_marginStart="8dp" 
    android:layout_marginTop="26dp" 
    android:background="@drawable/rounded_corner" 
    android:fontFamily="serif" 
    android:text="I have one listView " 
    android:textAlignment="textStart" 
    android:textColor="@color/bb_darkBackgroundColor" 
    android:textSize="15sp" 
    app:layout_constraintHorizontal_bias="0.0" 
    app:layout_constraintLeft_toRightOf="@+id/imageViewChat" 
    app:layout_constraintRight_toRightOf="parent" 
    app:layout_constraintTop_toTopOf="parent" 
    app:layout_constraintWidth_default="wrap"/> 

我想要的就是以编程方式删除的最后一行,因为有些时候我不“T需要

谢谢

回答

1

我的理解是,您想要将app:layout_constraintWidth_default="wrap"更改为app:layout_constraintWidth_default="spread"这是默认设置。

以下代码显示了如何使用ConstraintSet这样做。 XML正是您所呈现的,但ConstraintLayout已被授予constraintLayout的ID。

该代码抓取布局中存在的所有约束,进行所需更改并重新应用更改。

protected void onCreate(Bundle savedInstanceState) { 
    final ConstraintSet cs = new ConstraintSet(); 
    ConstraintLayout layout; 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    layout = (ConstraintLayout) findViewById(R.id.constraintLayout); 
    cs.clone(this, R.layout.activity_main); 
    cs.constrainDefaultWidth(R.id.textViewChat, ConstraintSet.MATCH_CONSTRAINT_SPREAD); 
    cs.applyTo(layout); 
}