TextView - detect if String exceeds available space

后端 未结 2 388
旧时难觅i
旧时难觅i 2021-01-26 16:09

Here is my layout

         RelativeLayout
             ImageView  iv1
             TextView  
                   toTheRightOf iv1
                   toTheEndOf           


        
相关标签:
2条回答
  • 2021-01-26 16:48

    It may be helpful. I did something like that, and in my solution I am scaling down textView, but you can set shorter text.

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

    0 讨论(0)
  • 2021-01-26 16:54

    Try this solution:

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

    In this layout, textview would ellipsize itself whenever the length would be greater than the available space.

    0 讨论(0)
提交回复
热议问题