2017-08-02 229 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); 
}