Android 删除以编程方式添加的RelativeLayout.END_
我试图从以编程方式添加的视图布局参数中删除Android 删除以编程方式添加的RelativeLayout.END_,android,android-layout,android-relativelayout,Android,Android Layout,Android Relativelayout,我试图从以编程方式添加的视图布局参数中删除RelativeLayout.END\u OF,但似乎仅仅编写viewLayoutParams.removule(RelativeLayout.END\u OF)是行不通的,我也不知道为什么 我正在尝试根据选项进行实时预览,这里是我的代码 XML: 它第一次工作(删除toEndOf layout参数),但下一次不会删除它,因为我是以编程方式添加参数的 更新 我需要将layout\u toEndOflayout参数添加到视图中,因为视图的默认位置与另一个视
RelativeLayout.END\u OF
,但似乎仅仅编写viewLayoutParams.removule(RelativeLayout.END\u OF)
是行不通的,我也不知道为什么
我正在尝试根据选项进行实时预览,这里是我的代码
XML:
它第一次工作(删除toEndOf layout参数),但下一次不会删除它,因为我是以编程方式添加参数的
更新
我需要将layout\u toEndOf
layout参数添加到视图中,因为视图的默认位置与另一个视图相关,它们应该在下一个视图中,并且在下一个视图之后,该视图可能位于其布局的中心,因此我应该删除的RelativeLayout.END\u,而是添加RelativeLayout.center\u水平
参数。很高兴知道这两个参数不能一起工作,
的RelativeLayout.END_比另一个参数更强大
屏幕截图
在选择选项之前:“4沙班1436”的默认位置是“伊斯兰”标签的末尾
选中选项后:“4沙板1436”未定位在中心位置,因为
参数的RelativeLayout.END_未正确删除,这就是问题所在
我终于找到了解决这个问题的方法。我应该使用新建RelativeLayout.LayoutParams()
为每个视图创建一个新的布局参数,如果布局无效会发生什么?删除。。。这是通过编程方式添加的。
=不要添加它。@Knossos我不确定,但我想我试过了,没有帮助。请给我一个你正在谈论的例子。@DerGolem我需要添加它,正如我在最近的更新中解释的那样。
<RelativeLayout
android:id="@+id/liveExample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/liveExampleLabel">
<TextView
android:id="@+id/iranianDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Monday, 8 Bahman 1370"
android:textAppearance="@style/AppTheme.Text.IranianDate"
tools:ignore="HardcodedText"/>
<TextView
android:id="@+id/islamicDate_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@id/iranianDate"
android:text="@string/islamic"
android:textAppearance="@style/AppTheme.Text.IslamicLabel"/>
<TextView
android:id="@+id/islamicDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8 Rajab 1436"
android:layout_below="@id/iranianDate"
android:textAppearance="@style/AppTheme.Text.IslamicDate"
tools:ignore="HardcodedText"/>
</RelativeLayout>
private void updateLivePreviewDatesLayout(boolean isChecked) {
RelativeLayout.LayoutParams iranianDateLayoutParams = (RelativeLayout.LayoutParams) iranianDate
.getLayoutParams();
RelativeLayout.LayoutParams islamicDateLayoutParams = (RelativeLayout.LayoutParams) islamicDate
.getLayoutParams();
if (isChecked) {
iranianDateLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
islamicDateLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
islamicDateLayoutParams.setMarginStart(0);
islamicDateLayoutParams.removeRule(RelativeLayout.END_OF);
} else {
iranianDateLayoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL);
islamicDateLayoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL);
if (displayIslamicLabel.isChecked()) {
islamicDateLayoutParams.setMarginStart(8);
islamicDateLayoutParams.addRule(RelativeLayout.END_OF,
R.id.islamicDate_label);
}
}
iranianDate.setLayoutParams(iranianDateLayoutParams);
islamicDate.setLayoutParams(islamicDateLayoutParams);
}
centerHorizontal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editor.putBoolean("w_centerHorizontal", isChecked).apply();
updateLivePreviewDatesLayout(isChecked);
}
});