development

프로그래밍 방식으로 Android RelativeLayout에서 "centerInParent"설정

big-blog 2020. 7. 2. 07:17
반응형

프로그래밍 방식으로 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);

이것이 도움이되기를 바랍니다.

참고URL : https://stackoverflow.com/questions/3985787/android-relativelayout-programmatically-set-centerinparent

반응형