2017-06-10 1 views
0

私はテキストの一部を表示するためにexpandable textviewを使用しています。ユーザーがこのtextviewをクリックすると、このexampleを使用していますが、問題は、拡張可能なテキストビューのトリム長さが固定に設定されていますが、私は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(); 
      } 
     }); 
ようにそれを行うことができます
関連する問題