以编程方式更改TextView对齐(Change TextView align programmatically)

我的RelativeLayout是这样的:

ImageView (向后箭头) TextView (标题) ImageButton (关闭按钮)

第一个ImageView只是一个箭头 TextView可以更改,并且必须保持在两个图像之间 第二个ImageView是一个关闭按钮

我正在尝试将第一张图像的可见性更改为GONE或VISIBLE

为此,我需要更新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); }

箭头背面是GONE和VISIBLE没有问题,但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>

My RelativeLayout is like this :

ImageView(arrow back) TextView(title) ImageButton(close button)

the first ImageView is just an arrow the TextView can change and must stay between the two image the second ImageView is a close button

I'm trying to change the visibility of the first image to GONE or VISIBLE

For this I need to update the layout param of the TextView, but this is not working properly

Here is my method :

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); }

The arrow back is GONE and VISIBLE with no problem but the textview just stack on it. RIGHT_OF is not working.

Any idea ?

The complete layout :

<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 GONE时,RelativeLayout将移动TextView以使其与parentLeft对齐。

像这样写你的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 。

You don't need to programmatically change the layout params of the TextView, use android:layout_alignWithParentIfMissing="true" instead. RelativeLayout will move the TextView to align it parentLeft when you make the first ImageView GONE.

Write your TextView like this:

<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" />

If this is NOT what you intended and you want the TextView to remain centered when you move the ImageView, then the solution is even simpler: set the ImageView's visibility to INVISIBLE, not GONE.

更多推荐