【问题标题】:Hide a view when textView is too long当 textView 太长时隐藏视图
【发布时间】:2016-07-05 09:56:40
【问题描述】:

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

先谢谢了,如果你想看,可以提供任何东西!

【问题讨论】:

  • 你目前的布局是什么?
  • @BartekLipinski 添加了
  • 也许您可以检查视图边界是否触摸?创建视图Rects,为其中一个添加所需的空间并检查它们intersect?

标签: android android-linearlayout textview android-view


【解决方案1】:

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

希望对你有所帮助。

简单!

【讨论】:

    【解决方案2】:

    (因为 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>
    

    小心layout_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

    它正在发挥作用:

    【讨论】:

    【解决方案3】:

    这是一种方式: 首先通过反复试验或其他方法确定有多少字符使字符串过长。

    然后用.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 使视图不可见但仍然存在。

    希望对你有帮助!!

    【讨论】:

    • 很高兴它成功了!感谢您承认它有效
    • 顺便说一句,您也可以通过类似的语句来反转它。 if (str.length()
    • 实际上更好的方法是 } else { v.setVisibility(View.VISIBLE); }
    • 如果字符串的长度回落到 9 以下,这显然会使视图重新出现
    • @BilalMH 如果用户手机的字体缩放设置为&gt;1.0 值,或者屏幕的大小与您的不同,这种方法将完全崩溃。
    【解决方案4】:

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

    【讨论】:

    • 我该怎么做?它只是 .getLength 或 .getPos 的情况吗?或者什么
    【解决方案5】:

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

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

    【讨论】:

    • 我不想限制用户名TextView,如果用户名太长我想隐藏日期textview
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 2015-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-05-31
    相关资源
    最近更新 更多