I was trying to create a constraint chain programmatically, using the code bellow.
class Table extends Component {
constructor (props) {
super(props);
this.state = {
table: this.createTable()
}
componentWillReceiveProps(nextProps) {
this.setState({table: this.createTable()});
}
createTable() {
// function using sizeX, sizeY and bombNumber
}
}
I have getting the following error:
bombNumber
Then I realized that the implementation of this method is as follow:
updateSize (val, side) {
this.setState({[side]: val}, function () {
this.setState({maxBombNumber: this.calculateMaxBombNumber()}, function () {
if (this.state.maxBombNumber < this.state.bombNumber) {
this.setState({bombNumber: this.state.maxBombNumber}, function (){
console.log(this.state);
});
}
});
});
}
Should I use set.createHorizontalChain(
ConstraintSet.PARENT_ID, ConstraintSet.START,
ConstraintSet.PARENT_ID, ConstraintSet.END,
new int[] {view1.getId(), view2.getId()},
null,
ConstraintSet.CHAIN_PACKED
);
which seems to work, since 6 and 7 are the values for Caused by: java.lang.IllegalArgumentException: Left to start undefined
at android.support.constraint.ConstraintSet.connect(ConstraintSet.java:922)
at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:883)
at android.support.constraint.ConstraintSet.createHorizontalChain(ConstraintSet.java:850)
and public void createHorizontalChain(int leftId, int leftSide, int rightId, int rightSide, int[] chainIds, float[] weights, int style) {
this.createHorizontalChain(leftId, leftSide, rightId, rightSide, chainIds, weights, style, 1, 2);
}
public void createHorizontalChainRtl(int startId, int startSide, int endId, int endSide, int[] chainIds, float[] weights, int style) {
this.createHorizontalChain(startId, startSide, endId, endSide, chainIds, weights, style, 6, 7);
}
meaning this is a bug, or I am doing something wrong here?
Seriously, dealing with createHorizontalChainRtl
programmatically has being really challenging.
答案 0 :(得分:5)
你应该改用
set.createHorizontalChain(
ConstraintSet.PARENT_ID, ConstraintSet.RIGHT,
ConstraintSet.PARENT_ID, ConstraintSet.LEFT,
new int[] {ITEM_ID_1, ITEM_ID_2},
null ,
ConstraintSet.CHAIN_SPREAD);
这将在所有项目之间创建一个链。如果您的项目是在XML中创建的,那么您可能需要清除已应用的ConstrainSet,方法是使用ConstrainSet.Clear(ITEM_ID)
获取新的约束集。然后开始新的约束集规则。
如果清除项目的constrainSet,则不要忘记添加
set.constrainWidth(ITEM_ID, ConstraintSet.WRAP_CONTENT);
set.constrainHeight(ITEM_ID, ConstraintSet.WRAP_CONTENT);
最后不要忘记.applyTo
set.applyTo(constraintLayout);
这是一个相当不错的例子,但是这个人过早地将所有项目连接起来,这是不必要的。 HorizontalChain Example
以下是有关chain styles
的详细信息Here is also a Wiki关于如何在代码中使用constrainlayout。