java - 以编程方式更改 TextView 对齐方式

标签 java android android-layout

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

向后的箭头消失并且可见,没有问题,但 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 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 .

关于java - 以编程方式更改 TextView 对齐方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23913880/

相关文章:

java - 无法单击未聚焦的 EditText 字段

java - ArrayList<String> 索引越界

java - 使用正则表达式修剪输入

android - 在线程问题中启动媒体播放器

android - 使用 RelativeLayout 参数时 View 重叠

android - GCM 云连接服务器 (CCS) 是否存储消息

android - 如何在Android中将图标垂直对齐到第一行textview的中心

java - BufferedOutputStream 未写入 standardIO

android - CardView cardUseCompatPadding

android - 如何自定义snackBar 的布局?