ConstraintLayout
에서 View
이 연결된 View
을 프로그래밍 방식으로 가져올 수 있습니다. 단, 두보기 ID View
이 다른 View
(예 : ConstraintSet.TOP
)에 어떻게 연결되어 있는지 압니다. 내 레이아웃에서 두 개의 (반드시 연결되어 있지 않은!) 뷰를 교환하고 싶습니다. ConstraintLayout에서 내보기가 연결되어있는보기를 결정하십시오 (예 : 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
의가있다. 그들은 할 수는 있지만 어떤 식 으로든 연결될 필요는 없습니다. - 첫 번째보기가 연결된보기입니다. 이것이 내 두 번째 견해가 연결되기를 바라는 것입니다.
- 그리고 내 두 번째보기가 연결된보기; 이것이 제가 처음으로 바라 보길 바라는 것입니다.
Think : 체스 보드에서 나는 두 구석을 교환하고 싶습니다. 나는 그들의 ID를 가지고 있으며 각각의 상단이 다른 관점에 연결되어 있다는 것을 알고 있습니다. 당신은 연결 유형을 알고 있다면 다음과 같이
다시 한 번! 음, 다시 한번 감사드립니다! – kalabalik