Java 以编程方式更改文本视图对齐

Java 以编程方式更改文本视图对齐,java,android,android-layout,Java,Android,Android Layout,我的RelativeLayout如下所示: ImageView(向后箭头)TextView(标题)ImageButton(关闭按钮) 第一个ImageView只是一个箭头 TextView可以更改,并且必须保持在两个图像之间 第二个ImageView是一个关闭按钮 我正在尝试将第一个图像的可见性更改为“已消失”或“可见” 为此,我需要更新TextView的布局参数,但这不能正常工作 以下是我的方法: private void setBackButton(boolean visible) {

我的
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只是堆叠在它上面。右箭头不起作用

有什么想法吗

完整布局:

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

如果您只是这样设置布局:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <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:gravity="center"
        android:singleLine="true"
        android:text="Title" />

    <ImageButton
        android:id="@+id/fragment_close"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

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

并通过编程更改mimagerrowback的可见性,它将起作用


图像视图
消失时,其布局不会膨胀,但会保留对视图的引用。因此,在这种情况下,
TextView
将与布局左侧对齐

您不需要以编程方式更改TextView的布局参数,而是使用
android:layout\u alignWithParentIfMissing=“true”
。RelativeLayout将在第一个ImageView
消失时移动TextView使其与parentLeft对齐

按如下方式编写文本视图:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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="50dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

    <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:gravity="center"
        android:singleLine="true"
        android:text="Title" />

    <ImageButton
        android:id="@+id/fragment_close"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />

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


如果这不是您想要的,并且您希望在移动ImageView时TextView保持居中,那么解决方案更简单:将ImageView的可见性设置为
不可见
,而不是
消失

如果您在消失和可见之间进行更改,则根本不需要更改布局。框架应该为您处理这个问题。在每种状态下,您试图获得什么效果?显示此设置的XML布局可能有助于更清楚地说明问题。我刚刚添加了布局我希望箭头消失或可见,如果箭头消失,文本视图将对齐父视图左视图;如果箭头可见,文本视图将对齐父视图左视图。首先,如果我在params.addRule中将ALIGN_PARENT_LEFT更改为ALIGN_PARENT_RIGHT,则调用该方法使箭头消失(这就是为什么在xml中textview是alignParentLeft的原因)。但是当它消失时,我需要在图像消失时左对齐父/左对齐:/这毫无意义,感谢我所需要的+1