Android RelativeLayout Программно установить «CenterinParent»
-
10-10-2019 - |
Вопрос
у меня есть 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. Целвертик
с истинный и ЛОЖЬ.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 - false.
addOrRemoveProperty(mView, RelativeLayout.CENTER_IN_PARENT, false);
Centerhorizontal - True.
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, true);
Центргоризонтал - ЛОЖЬ
addOrRemoveProperty(mView, RelativeLayout.CENTER_HORIZONTAL, false);
ЦелВертик - правда
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, true);
Целвертика - ЛОЖЬ
addOrRemoveProperty(mView, RelativeLayout.CENTER_VERTICAL, false);
Надеюсь, это поможет вам.
Не связан с StackOverflow