在ConstraintLayout
中,如果我有两个观看次数,我怎么能以编程方式获得View
与View
相关联的View
。 ids我知道我的View
如何与其他ConstraintSet.TOP
相关联(例如,void swapViews(ConstraintLayout constraintLayout, int view1ID, int view2ID) {
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(constraintLayout);
// How do I get the "anchor" or "connectedView" of a view with a specific constraint?
// Something like the following:
// int anchor1 = this.findViewById(view1ID).get...(ConstraintSet.TOP);
// int anchor2 = this.findViewById(view2ID).get...(ConstraintSet.TOP);
constraintSet.connect(view2ID, ConstraintSet.TOP, anchor1, ConstraintSet.TOP);
constraintSet.connect(view1ID, ConstraintSet.TOP, anchor2, ConstraintSet.TOP);
constraintSet.applyTo(constraintLayout);
}
)?我想在我的布局中交换两个(不一定是连接的!)视图。
View
要明确:此处涉及四个{{1}}:
想想:在国际象棋棋盘中,我想换掉两个角落。我有他们的ID,我知道他们的每个顶部都与其他视图相关联。
答案 0 :(得分:1)
如果您知道layout_constraintTop_toTopOf
或layout_constraintTop_toBottomOf
等连接类型,则可以按如下方式使用布局参数:
View v = findViewById(R.id.viewYouAreInterestedIn);
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams) v.getLayoutParams();
int topConnectionId = lp.topToTop;
您可以查看ConstraintLayout.LayoutParams的文档以获取更多信息。