可扩展的TextView,ExpandableTextView与Scroller类的使用
生活随笔
收集整理的這篇文章主要介紹了
可扩展的TextView,ExpandableTextView与Scroller类的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉載時請注明出處,尊重他人的勞動成果,謝謝。
廢話不多說,先上圖演示下成果(圖有些丑,別見怪):
最近一直在研究Scroller類的使用方法,看了很多遍別人的例子總是感覺不得要領,最后還是自己實踐一下,這個控件的靈感來源于stackoverflow上一個人的提問,就是這種可擴展的TextView,當然,人家那個很好看,那時候感覺很神奇,自從知道Scroller這個類之后就拿它來練練手吧,大伙可以隨意更改它的效果,配對了很好看的。
附上代碼:
package com.sahadev.sildingfinishlayout;import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.Scroller; import android.widget.TextView;public class ExpandableTextView extends RelativeLayout {private TextView mTextView;private Button mButton;private int mTextViewId = 567576458;// 這里注意不要隨便填一個簡單的數字,可能會和R中的ID沖突造成無效private Scroller mScroller;private int mHeight, mWidthMeasureSpec, mButtonHeight, paddingSize = 1;private boolean isExpanded, WSettedFlag, HSettedFlag, onceFlag;private int times = 2;// 縮小的倍數,默認2倍public ExpandableTextView(Context context) {this(context, null);}public ExpandableTextView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public ExpandableTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);mTextView = new TextView(context);mTextView.setId(mTextViewId);mButton = new Button(context);mButton.setText("擴 展");mScroller = new Scroller(context);LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);lp.addRule(RelativeLayout.BELOW, mTextViewId);lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);mButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {if (!isExpanded) {// 展開來mScroller.startScroll(0, mHeight / times, 0, mHeight / times);postInvalidate();isExpanded = true;} else {// 收回去mScroller.startScroll(0, mHeight, 0, -mHeight / times);postInvalidate();isExpanded = false;}}});addView(mTextView);addView(mButton, lp);}@Overridepublic void computeScroll() {super.computeScroll();if (mScroller.computeScrollOffset()) {mTextView.setHeight(mScroller.getCurrY());postInvalidate();return;}}public void setTimes(int times) {if (times == 0) {throw new ArithmeticException("倍數不能為0");}this.times = times;}public void setTextViewPadding(int pixels) {mTextView.setPadding(pixels, pixels, pixels, 0);paddingSize = pixels;}public void setButtonTips(CharSequence text) {mButton.setText(text);}public void setText(CharSequence text) {mTextView.setText(text);}public void setTextColor(int color) {mTextView.setTextColor(color);}public void setTextSize(float size) {mTextView.setTextSize(size);}public void setBackgroundColor(int color) {mTextView.setBackgroundColor(color);}public void setWidth(int width) {mWidthMeasureSpec = width;mTextView.setWidth(width - paddingSize * 2);WSettedFlag = true;}public void setHeight(int height) {mHeight = (height - mButtonHeight) * 2;HSettedFlag = true;}/* onMeasure方法在重繪的時候會一直被調用 */@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);/* 此步驟只用執行一次,獲取到textView的寬度以及Button的高度以及一些初始化的值 */if (!WSettedFlag) {WSettedFlag = true;mWidthMeasureSpec = mTextView.getMeasuredWidth();}if (!HSettedFlag) {HSettedFlag = true;mHeight = mTextView.getMeasuredHeight();OtherTools.showLog("mHeight----" + mHeight);}if (!onceFlag) {onceFlag = true;mButtonHeight = mButton.getMeasuredHeight();OtherTools.showLog("mButtonHeight----" + mButtonHeight);// mTextView.setHeight(mHeight / times + mButtonHeight >// heightMeasureSpec ? heightMeasureSpec - mButtonHeight : mHeight /// times);mTextView.setHeight(mHeight / times);}// int tempHeight = mHeight / 2 + mButtonHeight;// tempHeight = tempHeight > heightMeasureSpec ? heightMeasureSpec :// tempHeight;setMeasuredDimension(mWidthMeasureSpec, mButtonHeight + mTextView.getMeasuredHeight());} }<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/slide"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><com.sahadev.sildingfinishlayout.ExpandableTextViewandroid:id="@+id/hello"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="#a6c" /></RelativeLayout>
package com.sahadev.sildingfinishlayout;import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.Window;public class MainActivity extends Activity {private ExpandableTextView textView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);textView = (ExpandableTextView) findViewById(R.id.hello);textView.setText("近日,多家媒體報道,山西最大的民營企業聯盛集團董事長邢利斌被警方帶據分析,邢利斌這次被警方帶走,很可能和聯盛集團債臺高筑有關。不過短短幾年,山西煤老板為何就和金融機構從蜜月期走到了劍拔弩張的田地?");textView.setTextSize(30);textView.setTextColor(Color.WHITE);textView.setTextViewPadding(15);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {getMenuInflater().inflate(R.menu.main, menu);return true;}}
由于還不太會往GitHub上傳東西,大家就將就著把代碼拷貝運行吧,有什么疑問請在下面留言。
總結
以上是生活随笔為你收集整理的可扩展的TextView,ExpandableTextView与Scroller类的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于裁判文书与犯罪案例文本挖掘项目
- 下一篇: 笔记:python3编写的仿hubot的