我想以編程方式更改togglebutton的約束,但我已經設置了xml文件中togglebutton的約束。我需要的是當我單擊切換按鈕時,我想重寫的切換按鈕,即,當我點擊togglebutton1約束然後用togglebutton3的左側添加自togglebutton2左側的約束與marging 0如何以編程方式更改約束佈局的子視圖的約束
private ToggleButton toggleButton1;
private ToggleButton toggleButton2;
private ToggleButton toggleButton3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toggleButton1 = (ToggleButton) findViewById(R.id.toggleButton1);
toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
toggleButton3 = (ToggleButton) findViewById(R.id.toggleButton3);
CompoundButton.OnCheckedChangeListener compoundButton = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
ConstraintLayout constraintLayout = (ConstraintLayout) findViewById(R.id.constraintLayout);
ConstraintSet set = new ConstraintSet();
set.clone(constraintLayout);
set.connect(toggleButton3.getId(),ConstraintSet.LEFT,toggleButton2.getId(),ConstraintSet.LEFT,0);
set.applyTo(constraintLayout);
} else
// somecode
}
};
toggleButton1.setOnCheckedChangeListener(compoundButton);
}
希望有人能幫助我
第二次刪除setContentView之後,它也不起作用。 –
你想重繪佈局嗎? –
我想重寫佈局中的togglebutton約束 –