반응형
프로그래밍 방식으로 Android RelativeLayout에서 "centerInParent"설정
다음 과 같은 RelativeLayout 이 있습니다.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip">
<Button
android:id="@+id/negativeButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentLeft="true"
android:background="@drawable/black_menu_button"
android:layout_marginLeft="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="@+id/positiveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dip"
android:textColor="#ffffff"
android:layout_alignParentRight="true"
android:background="@drawable/blue_menu_button"
android:layout_marginRight="5dip"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
positiveButton
다음과 같은 효과 를 프로그래밍 방식으로 설정할 수 있기를 원합니다 .
android:layout_centerInParent="true"
프로그래밍 방식으로 어떻게 만들 수 있습니까?
완전히 테스트되지 않은,하지만이 해야 일 :
View positiveButton = findViewById(R.id.positiveButton);
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams)positiveButton.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
positiveButton.setLayoutParams(layoutParams);
android:configChanges="orientation|screenSize"
매니페스트에서 활동 내부에 추가
Reuben 응답에서 다른 특징을 추가하기 위해 조건에 따라이 규칙을 추가하거나 제거하기 위해 다음과 같이 사용합니다.
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) holder.txtGuestName.getLayoutParams();
if (SOMETHING_THAT_WOULD_LIKE_YOU_TO_CHECK) {
// if true center text:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
holder.txtGuestName.setLayoutParams(layoutParams);
} else {
// if false remove center:
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
holder.txtGuestName.setLayoutParams(layoutParams);
}
나는 해냈다
1. centerInParent
2. 센터 수평
3. centerVertical
와 진실 과 거짓 .private void addOrRemoveProperty(View view, int property, boolean flag){
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
if(flag){
layoutParams.addRule(property);
}else {
layoutParams.removeRule(property);
}
view.setLayoutParams(layoutParams);
}
메소드 호출 방법 :
centerInParent-true
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, true);
centerInParent-거짓
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
centerHorizontal-true
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
centerHorizontal-거짓
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
centerVertical-참
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
centerVertical-거짓
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
이것이 도움이되기를 바랍니다.
반응형
'development' 카테고리의 다른 글
Google Maps V3-특정 범위의 확대 / 축소 수준을 계산하는 방법 (0) | 2020.07.02 |
---|---|
iOS 앱에서 프로그래밍 방식으로 대상 버전 / 빌드 수를 표시하는 방법은 무엇입니까? (0) | 2020.07.02 |
Ruby 배열의 마지막 요소를 제외한 모든 요소 (0) | 2020.07.02 |
nltk.data.load로 english.pickle을 (를) 불러 오지 못했습니다. (0) | 2020.07.02 |
문자열로 PHP 클래스 속성 가져 오기 (0) | 2020.07.02 |