방명록
- [Android] 제약조건 프로그래밍 방식으로 변경하기 | How to move constraint programmatically2023년 10월 03일 22시 43분 02초에 업로드 된 글입니다.작성자: 핀수728x90반응형
제약조건 프로그래밍 방식으로 변경하기
updateLayoutParams를 이용한다.
yourView.updateLayoutParams<ConstraintLayout.LayoutParams> { topToBottom = targetViewId startToEnd = targetViewId bottomToTop = targetViewId // ... }
예시를 위해 텍스트 뷰 하나를 만들고,
제약조건을 아래와 같이 두었다.
<TextView android:id="@+id/testTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="sorry, can't use this right now" app:layout_constraintEnd_toStartOf="@+id/guideline6" app:layout_constraintStart_toStartOf="@+id/guideline3" app:layout_constraintTop_toBottomOf="parent" />
현재는 화면 바깥에 존재하도록 되어 있다.
아무 버튼이나 클릭하면 해당 텍스트 뷰의 제약조건이 변경되도록 해주었다.
private fun buttonClick() = View.OnClickListener { binding.testTextView.updateLayoutParams<ConstraintLayout.LayoutParams> { topToBottom = binding.mediumJoinTextLayout.id } }
제약조건이 변경되어 원하는 위치로 이동한 것을 확인할 수 있다.
공부하며 작성된 글이라 잘못된 정보가 있을 수 있습니다.
말씀해주시면 수정하겠습니다. 감사합니다.
References
아래 글을 참고하여 작성 되었습니다.
ConstraintLayout: change constraints programmatically
I need help with ConstraintSet. My goal is to change view's constraints in code, but I cant figure out how to do this right. I have 4 TextViews and one ImageView. I need to set ImageView constrain...
stackoverflow.com
728x90반응형'pinslog > Log.daily()' 카테고리의 다른 글
[Kotlin] 마지막 요소를 제거한 리스트를 얻고 싶을 때 (0) 2023.10.10 [Android] Dialog 외부 영역 터치 가능하도록 설정하기 (0) 2023.10.08 [Android] Databinding Format Character (0) 2023.10.02 [Android] 프로젝트 셋팅을 하며 생긴 오류 해결하기 (0) 2023.10.01 Crashlytics 추가하기 (0) 2023.09.24 다음글이 없습니다.이전글이 없습니다.댓글