当 textView 太长时隐藏视图

Hide a view when textView is too long

如果我的 textview 太长并且它开始触及它,我想隐藏一个视图,我该怎么做呢?所以例如这样是可以的:

但是当它是一个长用户名并且长度接近时间戳时我想隐藏时间戳,我该怎么做呢?我这辈子都想不出来!

编辑:我现在的布局是这样的

list_item_post.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#FFFFFF">

    <LinearLayout
        android:id="@+id/opBackground"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

            <TextView
                android:id="@+id/authorTextView"
                android:layout_width="wrap_content"
                android:layout_height="34dp"
                android:layout_gravity="start|center"
                android:layout_marginStart="10dp"
                android:layout_marginTop="15dp"
                android:background="@drawable/textview_corner_radius"
                android:fontFamily="sans-serif-condensed"
                android:paddingBottom="8dp"
                android:paddingEnd="18dp"
                android:paddingStart="18dp"
                android:paddingTop="8dp"
                android:textColor="@android:color/white"
                android:textStyle="bold"
                android:gravity="center" />

                <View
                    android:id="@+id/postTriangle"
                    android:layout_width="15dp"
                    android:layout_height="15dp"
                    android:background="@drawable/ic_purple_triangle"
                    android:layout_marginStart="12dp"
                    android:layout_marginTop="-5dp"/>

            </LinearLayout>

            <TextView
                android:id="@+id/dateTime"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="0.8"
                android:gravity="end"
                android:layout_gravity="center"
                android:textColor="@color/purple"
                android:layout_marginTop="8dp"
                android:textSize="12sp"/>

            <ImageButton
                android:id="@+id/morePostOptions"
                android:layout_marginEnd="8dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/ic_more"
                android:layout_gravity="center"
                android:layout_marginTop="8dp"/>
        </LinearLayout>

        <TextView
            android:id="@+id/postText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:layout_marginStart="27dp"
            android:layout_marginEnd="25dp"
            android:layout_marginBottom="25dp"
            android:textColor="@android:color/black"
            android:textSize="14sp"
            android:lineSpacingExtra="5dp"/>

        <ImageView
            android:id="@+id/imagePost"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="5dp" />


    </LinearLayout>


</LinearLayout>

先谢谢了,有什么想看的可以提供!

我想你可以得到你的名字TextView的位置和你的时间TextView的位置。然后检查名称视图是否覆盖了你的时间视图。

您可以检查文本视图的长度并根据您的要求限制它..

 Textview textView=  //your textview

 if(textView.getText().length()>20){
               //hide your date
             dateView.setVisibility(View.GONE);  
            }

这是一种方式: 先通过试错什么的确定有多少个字符使字符串过长。

然后用.length()告诉你TextView中字符串的大小

每次更新 TextView 时,您都可以使用 if 语句来表示这样的内容。

Textview tv = (TextView)findViewById(R.id.textv);
View v = findViewById(R.id.view1);

String str = tv.getText().toString();
if (str.length() > 9){
v.setVisibility(View.GONE);
}

我不确定布局的其余部分是什么,您可能需要使用 layout_weight 来帮助调整布局的其余部分,因为 "View.GONE" 会直接从布局中获取视图。 View.INVISIBLE 使视图不可见但仍然存在。

希望对您有所帮助!!

首先你要找出屏幕上单行中有多少个字符()然后检查你的名称字符串长度并比较两者。例如,单行大小为 50 个字符,您的名称字符串长度为 45 个字符,您的日期时间文本大小为 20 个字符,如果 45>(50-20) 那么您可以隐藏日期时间 TextView。

希望对您有所帮助。

简单!

(因为 Jon Halls 发现我对其他答案的评论令人反感,让我重新措辞并澄清一些事情)

  1. 我找到其他答案(在我写那个的时候出现),包括已接受的答案,超级脏。
  2. 他们依赖于吸引眼球的东西。
  3. 当用户使用与开发者不同的设备时,它们会崩溃。
  4. 我相信你应该使用以下方法。
  5. 不靠目测。

使用这样的布局:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/text_username"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical|left"/>

    <TextView
        android:id="@+id/text_timestamp"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:ellipsize="end"
        android:gravity="center_vertical|right"
        android:maxLines="1"/>
</LinearLayout>

CAREFULlayout_widthlayout_weightellipsizegravitymaxLines 参数非常重要.不要更改它们。

然后添加一个简单的 ellipsize 观察者到你的时间戳 TextView

timestampText = (TextView) findViewById(R.id.text_timestamp);
timestampText.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

    @Override
    public void onGlobalLayout() {
        Layout l = timestampText.getLayout();
        if (l != null) {
            for (int i = 0; i < l.getLineCount(); ++i) {
                if (l.getEllipsisCount(i) > 0) {
                    timestampText.setVisibility(View.INVISIBLE);
                    return;
                }
            }
            timestampText.setVisibility(View.VISIBLE);
        }
    }
});

记得在用完后删除您的 OnGlobalLayoutListener

它正在运行: