2017-06-10 52 views
0

我使用可扩展的textview来显示文本的一些部分,当用户点击这个textview然后用户可以看到整个字符串的那个文本, m使用这个example,但问题是可扩展的textview的修剪长度设置为固定的,但我想根据屏幕大小设置修剪长度动态只有一行,当我使用trim_length = 200显示的文本是3行,这里是我的代码...如何让可扩展的textview宽度设置其修剪长度为1行

ExpandableTextView.java

public class ExpandableTextView extends TextView { 
    private static final int DEFAULT_TRIM_LENGTH = 200; 
    private static final String ELLIPSIS = "....."; 

    private CharSequence originalText; 
    private CharSequence trimmedText; 
    private BufferType bufferType; 
    private boolean trim = true; 
    private int trimLength; 

    public ExpandableTextView(Context context) { 
     this(context, null); 
    } 

    public ExpandableTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 

     TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView); 
     this.trimLength = typedArray.getInt(R.styleable.ExpandableTextView_trimLength, DEFAULT_TRIM_LENGTH); 
     typedArray.recycle(); 

     setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       trim = !trim; 
       setText(); 
       requestFocusFromTouch(); 
      } 
     }); 
    } 

    private void setText() { 
     super.setText(getDisplayableText(), bufferType); 
    } 

    private CharSequence getDisplayableText() { 
     return trim ? trimmedText : originalText; 
    } 

    @Override 
    public void setText(CharSequence text, BufferType type) { 
     originalText = text; 
     trimmedText = getTrimmedText(text); 
     bufferType = type; 
     setText(); 
    } 

    private CharSequence getTrimmedText(CharSequence text) { 
     if (originalText != null && originalText.length() > trimLength) { 
      return new SpannableStringBuilder(originalText, 0, trimLength + 1).append(ELLIPSIS); 
     } else { 
      return originalText; 
     } 
    } 

    public CharSequence getOriginalText() { 
     return originalText; 
    } 

    public void setTrimLength(int trimLength) { 
     this.trimLength = trimLength; 
     trimmedText = getTrimmedText(originalText); 
     setText(); 
    } 

    public int getTrimLength() { 
     return trimLength; 
    } 
} 

expandableTextView expandableTextView =(EXPAN dableTextView)findViewById(R.id.lorem_ipsum); expandableTextView.setText(yourText);

回答

2

,你可以做这样的

public class ExpandableTextView extends TextView 
{ 
    // copy off TextView.LINES 
    private static final int MAXMODE_LINES = 1; 
// private OnExpandListener onExpandListener; 
    private final int maxLines; 
    private boolean expanded; 


    public ExpandableTextView(final Context context) 
    { 
     this(context, null); 
    } 

    public ExpandableTextView(final Context context, final AttributeSet attrs) 
    { 
     this(context, attrs, 0); 
    } 

    public ExpandableTextView(final Context context, final AttributeSet attrs, final int defStyle) 
    { 
     super(context, attrs, defStyle); 

     // read attributes 
     final TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.ExpandableTextView, defStyle, 0); 
//  this.animationDuration = attributes.getInt(R.styleable.ExpandableTextView_trimLength, 200); 
     attributes.recycle(); 

     // keep the original value of maxLines 
     this.maxLines = this.getMaxLines(); 


    } 

    @Override 
    public int getMaxLines() 
    { 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) 
     { 
      return super.getMaxLines(); 
     } 

     try 
     { 
      final Field mMaxMode = TextView.class.getField("mMaxMode"); 
      mMaxMode.setAccessible(true); 
      final Field mMaximum = TextView.class.getField("mMaximum"); 
      mMaximum.setAccessible(true); 

      final int mMaxModeValue = (int) mMaxMode.get(this); 
      final int mMaximumValue = (int) mMaximum.get(this); 

      return mMaxModeValue == MAXMODE_LINES ? mMaximumValue : -1; 
     } 
     catch (final Exception e) 
     { 
      return -1; 
     } 
    } 

    public boolean toggle() 
    { 
     return this.expanded 
       ? this.collapse() 
       : this.expand(); 
    } 


    public boolean expand() 
    { 
     if (!this.expanded && this.maxLines >= 0) 
     { 

      // set maxLines to MAX Integer, so we can calculate the expanded height 
      this.setMaxLines(Integer.MAX_VALUE); 


      // keep track of current status 
      ExpandableTextView.this.expanded = true; 


      return true; 
     } 

     return false; 
    } 


    public boolean collapse() 
    { 
     if (this.expanded && this.maxLines >= 0) 
     { 

      ExpandableTextView.this.setMaxLines(ExpandableTextView.this.maxLines); 
      // keep track of current status 
      ExpandableTextView.this.expanded = false; 


      return true; 
     } 

     return false; 
    } 
} 

activity.xml

<com.yourpackagename.ExpandableTextView 
      android:id="@+id/expandableTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:ellipsize="end" 
      android:maxLines="1" 
    /> 

activity.java

ExpandableTextView expandableTextView = (ExpandableTextView) this.findViewById(R.id.expandableTextView); 
expandableTextView.setOnClickListener(new View.OnClickListener() 
     { 
      @SuppressWarnings("ConstantConditions") 
      @Override 
      public void onClick(final View v) 
      { 
       expandableTextView.toggle(); 
      } 
     }); 
相关问题