How to set maxLines and ellipsize of a TextView at the same time

前端 未结 7 1664
挽巷
挽巷 2021-01-30 12:54

I want to limit my text view to have maximum of 5 lines, so I did:



        
相关标签:
7条回答
  • 2021-01-30 13:03

    Progrmatically, you can use:

    your_text_view.setEllipsize(TextUtils.TruncateAt.END);
    your_text_view.setMaxLines(4);
    your_text_view.setText("text");  
    
    0 讨论(0)
  • 2021-01-30 13:03

    Apparently you have to specify the inputType and set it to none in this case. It should look something like this:

    <TextView
      android:ellipsize="end"
      android:inputType="none"
      android:maxLines="2"
    
    0 讨论(0)
  • 2021-01-30 13:04

    try this code...

        final TextView tv_yourtext = (TextView)findViewById(R.id.text);
    
        tv_yourtext.setText("A really long text");
        ViewTreeObserver vto = tv_yourtext.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
            @Override
            public void onGlobalLayout() {
                ViewTreeObserver obs = tv_yourtext.getViewTreeObserver();
                obs.removeGlobalOnLayoutListener(this);
                if(tv_yourtext.getLineCount() > 6){
                    Log.d("","Line["+tv_yourtext.getLineCount()+"]"+tv_yourtext.getText());
                    int lineEndIndex = tv_yourtext.getLayout().getLineEnd(5);
                    String text = tv_yourtext.getText().subSequence(0, lineEndIndex-3)+"...";
                    tv_yourtext.setText(text);
                    Log.d("","NewText:"+text);
                }
    
            }
        });
    
    0 讨论(0)
  • 2021-01-30 13:08
    <TextView
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"  
    android:maxLines="5"
    android:ellipsize="end" />
    

    Works for me, make sure TextView top container should have height like

    android:layout_height="wrap_content"
    
    0 讨论(0)
  • 2021-01-30 13:10

    When I set the TextView height to a constant value (like 50dp), it works fine for me.

    0 讨论(0)
  • 2021-01-30 13:21

    There is same question android ellipsize multiline textview This is known bug, quite old and not fixed yet :(

    someone posted workaround http://code.google.com/p/android-textview-multiline-ellipse/ maybe it will help you (or someone else)

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