textview点击展开全部或收起,内容过长显示省略号,设置行间距,字间距,跑马灯显示
生活随笔
收集整理的這篇文章主要介紹了
textview点击展开全部或收起,内容过长显示省略号,设置行间距,字间距,跑马灯显示
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?跑馬燈顯示
?
android:ellipsize="marquee" android:singleLine="true" paomad.setSelected(true);?
使用RelativeLayout可以使用圖標點擊旋轉,展開textview或收縮textview
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginLeft="15dp"android:layout_marginRight="6dp"android:layout_marginTop="7dp"android:layout_marginBottom="7dp"android:gravity="center_vertical"><TextViewandroid:id="@+id/onete"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentStart="true"android:layout_centerVertical="true"android:text="@string/wgms"android:textColor="@color/numtext"android:textSize="15dp" /><TextViewandroid:id="@+id/wgmiaostext"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="80dp"android:ellipsize="end"android:lineSpacingExtra="4dp"android:lines="2"android:text="申報過期申報報過期申報過期申過期申報過期申報過期申報報報報報過期申報過期"android:textColor="@color/hometextc"android:textSize="15dp" /><ImageViewandroid:id="@+id/more_image"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentEnd="true"android:layout_alignParentBottom="true"android:src="@mipmap/xj_03"/></RelativeLayout>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)資源
<color name="color_8290AF">#8290AF</color> <color name="color_232323">#232323</color>attrs.xml
<?xml version="1.0" encoding="utf-8"?> <resources><declare-styleable name="ExpandTextView"><attr name="showLines" format="integer"/></declare-styleable></resources>layout_expand_text.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/contentText"android:layout_width="match_parent"android:layout_height="wrap_content"android:textColor="@color/color_232323"android:textSize="14sp"android:text=""/><TextViewandroid:id="@+id/textState"android:layout_width="match_parent"android:layout_height="wrap_content"android:textSize="14sp"android:textColor="@color/color_8290AF"android:paddingTop="5dp"android:paddingBottom="5dp"android:text=""/></LinearLayout>(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() {@Overridepublic 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() {@Overridepublic 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);} }調用:
<com.example.my35.ExpandTextViewandroid:id="@+id/expand_textView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginLeft="10dp"android:ellipsize="end"android:lineSpacingExtra="3dp"android:maxLines="5"android:textSize="16sp"app:layout_constraintRight_toRightOf="parent"/> 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); //字間距總結
以上是生活随笔為你收集整理的textview点击展开全部或收起,内容过长显示省略号,设置行间距,字间距,跑马灯显示的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 解决sns加载数据load_datase
- 下一篇: 程序实践:命令行之连连看