繁体   English   中英

以编程方式更改TextView对齐

[英]Change TextView align programmatically

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

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

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

并以编程方式更改mImageArrowBack的可见性,它将起作用。

ImageView为GONE时,其布局不会放大,但会保留对View的引用。 因此,在这种情况下, TextView对齐到布局的左侧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM