正好朋友项目里遇到了给写了个小Demo:

这个监听器看名字也知道了,就是在绘画完成之前调用的,在这里面可以获取到行数,当然也可以获取到宽高等信息



package com.example.textviewtest;import android.annotation.SuppressLint;import android.app.Activity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewTreeObserver;import android.widget.Button;import android.widget.TextView;public class MainActivity extends Activity {private TextView text;private Button button;@SuppressLint("NewApi")@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);text = (TextView) findViewById(R.id.text2);button = (Button) findViewById(R.id.button);text.setText("广西新闻网-南国今报柳州讯 一消费者到发廊美发,因对美发效果不满,索要数千至1万元赔偿,并经工商调解不成,进而大"+ "闹发廊骚扰店主,并惊动了警方。警方介入耐心做工作,消费者在警方及工商见证后,接受店主提出的赔偿方案,双方化干戈为玉帛");//获取视图树的全局事件改变时得到通知ViewTreeObserver vto = text.getViewTreeObserver();//监听获取回掉函数vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {@Overridepublic boolean onPreDraw() {//获取text View 的高度int lineCount = text.getLineCount();System.out.println(lineCount);//逻辑判断,如果大于2显示按钮,如果行数小于或者等于2 则隐藏。if(lineCount>2){button.setVisibility(View.VISIBLE);}else{button.setVisibility(View.GONE);}return true;}});button.setOnClickListener(new OnClickListener() {Boolean flag = true;@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubLog.i("zkk", text.getHeight() + "");if (flag) {flag = false;text.setEllipsize(null);// 展开text.setSingleLine(flag);button.setText("隐藏");} else {flag = true;text.setMaxLines(2);// 收缩button.setText("显示");//text.setEllipsize(TruncateAt.END);}}});}}

下面是布局界面


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    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" >    <TextView        android:id="@+id/text1"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/text1"         android:layout_marginTop="10dp">        <TextView            android:id="@+id/text2"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:maxLines="2"             />                <Button            android:id="@+id/button"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_alignParentRight="true"            android:layout_marginTop="-10dp"            android:text="下拉" />    </RelativeLayout></RelativeLayout>


更多相关文章

  1. 如何获取android的相关信息,以及安装路径等信息的获取等
  2. Android常用代码之APK root权限静默安装
  3. 安卓截图笔记
  4. 关于Android或Java中获取系统时间问题
  5. android中获取设备的型号和手机厂商
  6. dumpsys的使用笔记
  7. Android两个Activity传递数据,onActivityResult获取结果时Intent
  8. Android采坑记录-自动更新APK出现No Activity found to handle I
  9. Android(安卓)获取 AudioRecord 麦克风音量大小并做选择性发送

随机推荐

  1. Android应用坐标系统全面详解
  2. Android(安卓)armeabi的so库兼容问题
  3. Android实现自定义View的自定义属性的一
  4. Android媒体播放---Media playback(二)
  5. mars老师推荐的学习android路线
  6. 电池寿命:Android“电池”工具是如何工作
  7. [Android]单机斗地主,破解版,无广告
  8. 关于从Eclipse导入项目到android studio
  9. 基于华视身份证读卡器读取身份证信息的An
  10. 想写出更好的应用?Google“Android Traini