textview点击展开全部或收起,内容过长显示省略号,设置行间距,字间距

使用RelativeLayout可以使用图标点击旋转,展开textview或收缩textview

 



    

    
    
    


Java代码如下

 

private boolean ifupdown=true;
@OnClick({R.id.backligxwbdata,R.id.more_image})
public void onClick(View view) {
    switch (view.getId()) {
        case R.id.backligxwbdata:
            onBackPressed();
            break;
        case R.id.more_image:
            if (ifupdown){
                Animation anim =new RotateAnimation(0f, 180f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setFillAfter(true); // 设置保持动画最后的状态
                anim.setDuration(500); // 设置动画时间
                anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
                more_image.startAnimation(anim);//图标旋转向上或向下
                ifupdown = !ifupdown;

                double d = (double) weiguidesc.length() / 18;//文本长度除以每行字符长度
                int  okcprogress = (int) (Math.floor(d))+1;//除数取整,也就是行数
                Log.i("lgq","lllll===-"+weiguidesc.length()+"......."+okcprogress);
                wgmiaostext.setLines(okcprogress);//展开全部
            }else {
                Animation anim =new RotateAnimation(180f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
                anim.setFillAfter(true); // 设置保持动画最后的状态
                anim.setDuration(500); // 设置动画时间
                anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
                more_image.startAnimation(anim);
                wgmiaostext.setLines(2);//收缩为原始两行               ifupdown = !ifupdown;
            }
            break;

    }
}

实现展开收起方法2:

(1)资源

#8290AF
#232323

attrs.xml



    
        
    


layout_expand_text.xml




    
    

(2)自定义textview

public class ExpandTextView extends LinearLayout {
    public static final int DEFAULT_MAX_LINES = 3;//最大的行数
    private TextView contentText;
    private TextView textState;

    private int showLines;

    private ExpandStatusListener expandStatusListener;
    private boolean isExpand;

    public ExpandTextView(Context context) {
        super(context);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(attrs);
        initView();
    }

    public ExpandTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttrs(attrs);
        initView();
    }

    private void initView() {
        setOrientation(LinearLayout.VERTICAL);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_expand_text, this);
        contentText = (TextView) findViewById(R.id.contentText);
        contentText.setLineSpacing(8,1);//行间距
        contentText.setLetterSpacing(0.5f);          //字间距
        if(showLines > 0){
            contentText.setMaxLines(showLines);
        }

        textState = (TextView) findViewById(R.id.textState);
        textState.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String textStr = textState.getText().toString().trim();
                if("全文".equals(textStr)){
                    contentText.setMaxLines(Integer.MAX_VALUE);
                    textState.setText("收起");
                    setExpand(true);
                }else{
                    contentText.setMaxLines(showLines);
                    textState.setText("全文");
                    setExpand(false);
                }
                //通知外部状态已变更
                if(expandStatusListener != null){
                    expandStatusListener.statusChange(isExpand());
                }
            }
        });
    }

    private void initAttrs(AttributeSet attrs) {
        TypedArray typedArray = getContext().getTheme().obtainStyledAttributes(attrs, R.styleable.ExpandTextView, 0, 0);
        try {
            showLines = typedArray.getInt(R.styleable.ExpandTextView_showLines, DEFAULT_MAX_LINES);
        }finally {
            typedArray.recycle();
        }
    }

    public void setText(final CharSequence content){
        contentText.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                // 避免重复监听
                contentText.getViewTreeObserver().removeOnPreDrawListener(this);

                int linCount = contentText.getLineCount();
                if(linCount > showLines){

                    if(isExpand){
                        contentText.setMaxLines(Integer.MAX_VALUE);
                        textState.setText("收起");
                    }else{
                        contentText.setMaxLines(showLines);
                        textState.setText("全文");
                    }
                    textState.setVisibility(View.VISIBLE);
                }else{
                    textState.setVisibility(View.GONE);
                }
                return true;
            }


        });
        contentText.setText(content);
    }

    public void setExpand(boolean isExpand){
        this.isExpand = isExpand;
    }

    public boolean isExpand(){
        return this.isExpand;
    }

    public void setExpandStatusListener(ExpandStatusListener listener){
        this.expandStatusListener = listener;
    }

    public static interface ExpandStatusListener{

        void statusChange(boolean isExpand);
    }
}

调用:

ExpandTextView textView1 =findViewById(R.id.expand_textView);
textView1.setText("分为非我仿佛威风威风为任务范围范围分为非危房危房危房威锋网分为非仍无法" +
        "为非危房危房危房威锋网分为非仍无法危房违法未范围范围分为非危房危房任务分为分为分为我分为非我仿佛威风威" +
        "风为任务范围范围分为非危房危房危房威锋网分为非仍无法危房违法未范围范围分为非危房危房任务分为分为分为我");

单行省略

android:singleLine="true"
android:ellipsize="end"


多行省略

android:maxLines="3"
android:ellipsize="end"


行距

 

android:lineSpacingExtra="6dp"

contentText.setLineSpacing(8,1);//行间距
contentText.setLetterSpacing(0.5f);          //字间距

 

你可能感兴趣的:(移动开发,Android开发)