TextView - 检测String是否超出可用空间

时间:2017-05-29 12:59:44

标签: android android-layout

这是我的布局

         RelativeLayout
             ImageView  iv1
             TextView  
                   toTheRightOf iv1
                   toTheEndOf   iv1
                   toTheLeftOf  iv2      
             ImageView  iv2
         RelativeLayout end

所以TextView夹在两个ImageView之间。所以一切都准确地显示出来。但是,有时TextView中的行太长并且太靠近第二个ImageView。有时延伸到它。我不打算改变布局或试图挤压任何东西。我想要做的是检测String的长度是否太长,并将String的内容更改为具有较少字符的缩写版本。如何判断String是否以及何时离ImageView太近?有时有空间,有时没有。这个布局适用于RecyclerView中的项目 - 就问题而言,这并不重要。我知道Android可以检测到String太长并且添加...但我想要做的不是...我想简单地将String更改为具有更少字符的String。

也许我只需要决定多少个字符太多了?但这会因设备分辨率等而改变吗?或者我是在平板电脑或Galaxy Note或更小的设备上是否相同?我不想为每个设备设置不同的布局。我宁愿动态地检查字符串是否会占用太多空间。在任何情况下,它都不是布局更改,而是我追求的字符串内容更改。

更新:澄清。

让我们说字符串是'2017年12月19日星期四' 如果这会占用太多空间,那么我不想看到     '星期四,十二月十九日......' 相反,我想看到字符串太长而不是

    tv.setText('12/19/2017');

我想根据给定字符串是否占用太多空间来有条件地设置文本值。

2 个答案:

答案 0 :(得分:2)

试试这个解决方案:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image1"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="dsajkhfkjhfasa fbsa fas fahj afs fsa afs sf afs sda da das dsals"
        android:ellipsize="middle"
        android:padding="10dp"
        android:maxLines="1"
        android:layout_toLeftOf="@+id/image2"
        android:layout_toRightOf="@+id/image1" />

    <ImageView
        android:id="@+id/image2"
        android:layout_alignParentRight="true"
        android:src="@android:drawable/ic_dialog_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

在这种布局中,只要长度大于可用空间,textview就会自动化。

答案 1 :(得分:2)

它可能会有所帮助。我做了类似的事情,在我的解决方案中,我缩小了textView,但你可以设置更短的文本。

    if(textView.getWidth() > 0) {
        float widthOfTheText = textView.getPaint().measureText(textView.getText());
        if(widthOfTheText > textView.getWidth()) {
            //exceeds, let scale it
            textView.setTextScaleX((textView.getWidth()-0.1f)/widthOfTheText);
        } else {
            //not exceeds
        }
    } else {
        //not layouted yet
        textView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
              TextView tv = (TextView) v;
              float widthOfTheText = tv.getPaint().measureText(tv.getText());
              tv.setTextScaleX((tv.getWidth()-0.1f)/widthOfTheText);
              tv.removeOnLayoutChangeListener(this);

              if(widthOfTheText > tv.getWidth()) {
                 //exceeds, let scale it
                 tv.setTextScaleX((textView.getWidth()-0.1f)/widthOfTheText);
              } else {
                 //not exceeds
              }
           }
       });
    }

Result