自定义可以伸缩的TextView

在android界面开发中,我们经常会用到可伸缩的TextView,下面我们就来写一个可以伸缩的TextView,效果如图所示:
![拉伸前](https://static.oschina.net/uploads/img/201601/30215257_4fqM.jpg "在这里输入图片标题")
![拉伸后](https://static.oschina.net/uploads/img/201601/30215328_5udc.jpg "在这里输入图片标题")

因为整个拉伸部分有Button和TextView,所以我们选择集成LinearLayout来实现ExpandView。
布局文件如下:
```

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="群简介"
        android:layout_weight="1"
        android:layout_gravity="center_vertical"
        android:drawableLeft="@mipmap/community_intro"
        android:drawablePadding="8dp"
        android:id="@+id/textView44"
        android:layout_marginLeft="20dp"
        android:textSize="17sp"/>

    <ImageButton
        android:layout_gravity="center_vertical|right"
        android:background="@null"
        android:src="@mipmap/community_edit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_community_edit"
        android:layout_marginRight="20dp"/>
</LinearLayout>
<View
    android:background="#c6c6c6"
    android:layout_width="match_parent"
    android:layout_height="0.8dp"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:orientation="vertical"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:text="群简介"
    android:id="@+id/tv_conmmunity_info"
    android:paddingLeft="48dp"
    android:paddingRight="50dp"
    android:layout_marginTop="8dp"
    android:textSize="10sp"/>

<ImageButton
    android:background="@null"
    android:src="@mipmap/conmmunity_info_more"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:id="@+id/btn_info_more"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="15dp"/>

</LinearLayout> ```

最重要的部分,继承LiearLayout


```

/**Created by lenovo on 2016/1/30.

  • 可伸缩的TextView */ public class ExpandView extends LinearLayout{

    private TextView tvContent; private ImageButton btnMore; private ImageButton btnEdit; private boolean isExpand = false; //true代表是展开状态,false代表折叠状态 private static final int durationMillis = 350;//动画持续时间 int maxLines;//最大行数

    public ExpandView(Context context) { super(context); }

    public ExpandView(Context context, AttributeSet attrs) { super(context, attrs); initContentView(context, attrs); }

    protected void initContentView(Context pContext,AttributeSet pAttributeSet) { TypedArray lTypedArray = pContext.obtainStyledAttributes(pAttributeSet, R.styleable.ExpandView); int textColor = lTypedArray.getColor(R.styleable.ExpandView_text_color, Color.BLACK); int textSize = lTypedArray.getDimensionPixelSize(R.styleable.ExpandView_text_size, DisplayUtils.dip2px(6,getContext())); String textContent = lTypedArray.getString(R.styleable.ExpandView_text_content); maxLines = lTypedArray.getInt(R.styleable.ExpandView_maxLine, 4); lTypedArray.recycle(); View contentView = LayoutInflater.from(pContext).inflate(R.layout.expand_layout,this,false); this.addView(contentView, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); tvContent = (TextView) contentView.findViewById(R.id.tv_conmmunity_info); btnMore = (ImageButton) contentView.findViewById(R.id.btn_info_more); btnEdit = (ImageButton) contentView.findViewById(R.id.btn_community_edit); initContentView(maxLines,textContent,textColor,textSize); bindListener(); }

    protected void initContentView(final int maxLines,String textContent,int textColor,int textSize){ tvContent.setHeight(maxLines * tvContent.getLineHeight()); tvContent.setText(textContent); tvContent.setTextColor(textColor); tvContent.setTextSize(textSize); post(new Runnable() { @Override public void run() { btnMore.setVisibility(tvContent.getLineCount() > maxLines ? View.INVISIBLE : View.GONE); } }); }

    protected void bindListener() { ExpandListener lExpandListener = new ExpandListener(); tvContent.setOnClickListener(lExpandListener); btnMore.setOnClickListener(lExpandListener); }

    //获取简介TextView的引用 public TextView getTextView() { return tvContent; }

    //获取点击更多的ImageButton引用 public ImageButton getBtnMore() { return btnMore; }

    //获取编辑按钮 public ImageButton getBtnEdit() { return btnEdit; }

    //设置简介的内容 public void setTvContent(CharSequence pCharSequence) { if (!TextUtils.isEmpty(pCharSequence)){ tvContent.setText(pCharSequence); }else{ throw new NullPointerException("简介设置不能为空"); } }

    class ExpandListener implements View.OnClickListener {

     @Override
     public void onClick(View v) {
         tvContent.clearAnimation();
         final int deltaValue;//折叠动画的差值
         final int startValue = tvContent.getHeight();
         Log.e("高度", "onClick: "+startValue);
         //展开动画
         if (!isExpand){
             deltaValue = tvContent.getLineHeight()*tvContent.getLineCount()-startValue;
             RotateAnimation lRotateAnimation = new RotateAnimation(0, 180, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
             AccelerateInterpolator lAccelerateInterpolator = new AccelerateInterpolator();
             lRotateAnimation.setDuration(durationMillis);
             lRotateAnimation.setFillAfter(true);
             lRotateAnimation.setInterpolator(lAccelerateInterpolator);
             btnMore.startAnimation(lRotateAnimation);
         }
         //折叠动画
         else{
             deltaValue = tvContent.getLineHeight() * maxLines  - startValue;
             RotateAnimation lRotateAnimation = new RotateAnimation(180,0,Animation
                     .RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
             AccelerateInterpolator lAccelerateInterpolator = new AccelerateInterpolator();
             lRotateAnimation.setDuration(durationMillis);
             lRotateAnimation.setFillAfter(true);
             lRotateAnimation.setInterpolator(lAccelerateInterpolator);
             btnMore.startAnimation(lRotateAnimation);
         }
    
         Animation animation = new Animation()  
         {
             protected void applyTransformation(float interpolatedTime, Transformation t) {
                 tvContent.setHeight((int) (startValue + deltaValue * interpolatedTime));
             }
         };
    
         animation.setDuration(durationMillis);
         tvContent.startAnimation(animation);
         isExpand = !isExpand;
     }
    

    }

}

    styleable文件如下:

    ```
<resources>
    <declare-styleable name="ExpandView">
        <attr name="text_content" format="string"/>
        <attr name="text_color" format="color"/>
        <attr name="text_size" format="dimension"/>
        <attr name="maxLine" format="integer"/>
    </declare-styleable>
</resources>

转载于:https://my.oschina.net/lengwei/blog/611829

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值