我RelativeLayout
是这样的:
ImageView
(后退箭头)TextView
(标题)ImageButton
(关闭按钮)
ImageView
只是一支箭TextView
可以改变,必须保持两个图像之间ImageView
是关闭按钮我正在尝试将第一张图片的可见性更改为“消失”或“可见”
为此,我需要更新的布局参数TextView
,但这不能正常工作
这是我的方法:
private void setBackButton(boolean visible) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) mTitle.getLayoutParams();
if (visible) {
mImageArrowBack.setVisibility(View.VISIBLE);
params.addRule(RelativeLayout.RIGHT_OF, R.id.fragment_back);
} else {
mImageArrowBack.setVisibility(View.GONE);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
params.addRule(RelativeLayout.LEFT_OF, R.id.fragment_close);
mTitle.setLayoutParams(params);
}
向后的箭头消失了并且没有问题,但是textview只是堆叠在上面。RIGHT_OF不起作用。
任何的想法 ?
完整的布局:
<RelativeLayout
android:id="@+id/fragment_title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/fragment_back"
android:layout_width="@dimen/button_favorite"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:src="@drawable/arrow_left" />
<TextView
android:id="@+id/fragment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/fragment_list_country_close"
android:singleLine="true" />
<ImageButton
android:id="@+id/fragment_close"
android:layout_width="@dimen/button_favorite"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/close_btn" />
</RelativeLayout>
您无需以编程方式更改TextView的布局参数,android:layout_alignWithParentIfMissing="true"
而可以使用。当您制作第一个ImageView时,RelativeLayout将移动TextView使其与parentLeft对齐GONE
。
像这样编写您的TextView:
<TextView
android:id="@+id/fragment_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/fragment_back"
android:layout_toLeftOf="@+id/fragment_close"
android:layout_alignWithParentIfMissing="true"
android:singleLine="true" />
如果这不是您想要的,并且您希望TextView在移动ImageView时保持居中,则解决方案甚至更简单:将ImageView的可见性设置为INVISIBLE
,而不是GONE
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句