Android在onCreate()方法中动态获取TextView控件的高度
正好朋友项目里遇到了给写了个小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")
@Override
protected 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() {
@Override
public 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; @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.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>
Android在onCreate()方法中动态获取TextView控件的高度的更多相关文章
- Android查缺补漏(View篇)--在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0?
在 Activity 的 onCreate() 方法中为什么获取 View 的宽和高为0 ? @Override protected void onCreate(Bundle savedInstanc ...
- 谨记给UpdatePanel中动态添加的控件赋ID
原文:谨记给UpdatePanel中动态添加的控件赋ID 昨天下定决 心对上次做的布局编辑器控件加以改进,其中最主要变化的就是要完全使用ASP.NET AJAX!但是很遗憾,虽然耳闻已久,但目前对AS ...
- android自定义控件(6)-详解在onMeasure()方法中如何测量一个控件尺寸
今天的任务就是详细研究一下protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法.如果只是说要重写什么方法有什么 ...
- android中动态修改ImageView控件的宽高度
本例实现了动态修改ImageView控件的宽高度,有两个按钮,一个按钮实现放大image,一个按钮实现缩小image activity_main.xml <?xml version=" ...
- Asp.net中从后台中如何获取html控件
比如定义html的文本框如下: <input type="text" onclick="fun()" id="htmlTxtDate" ...
- UGUI 之获取当前控件的高度
当Canvas Scaler选择Constant Pixel Size 当前的分辨率会被被固定,可以用RectTransform类里面的.rect变量值获取 height或Width. 在次情况下获取 ...
- Android动态布局,并动态为TextView控件设置drawableLeft、drawableRight等属性加入图标
注:(图中每个条目和图标都是由代码动态生成) 代码动态布局,并须要为每个条目设置图标,此时用到了 android:drawableLeft="@drawable/icon" 父x ...
- Android使用的webcview中带有音乐播放控件,在关闭或分享时处于界面不可见状态下,声音仍在播放的问题解决
一. 问题出现原因 我们在做APP分享时,分享webview加载带有音乐播放控件的网页.当弹出分享界面,webview的网页处于后台状态或关闭该网页时,音乐声仍在播放.出现该类现象使我 ...
- 动态为TextView控件设置drawableLeft图标,并设置间距
效果图: 重要属性: textView.setCompoundDrawablePadding(4);//设置图片和text之间的间距 textView.setPadding(-5, 0, 0, 0); ...
随机推荐
- uploadify插件可选参数的详细介绍
Uploadify 是一个JQuery插件,它协助你轻松简单的将一个或多个文件上传至你的网站. 它需要Flash控件和后台开发语言的支持,丰富的参数配置,同时也简单易用,让你轻松上手. 官 ...
- ArrayList中removeAll和clear的区别(无区别)
removeAll会直接调用此方法,传入list和false,因中间的逻辑都不会走(如果由retainAll方法调用,则会走这些逻辑判断),所以只需要看finaly中的最后一个if条件,w=0,通过循 ...
- Chrome插件:浏览器后台与页面间通信
content.js 与 background.js和popup.js 通信和 background.js与popup.js 这些通信都用 chrome.runtime.sendMessage 这个 ...
- 1 SQL 数据库和SQL
1 数据库和SQL 1-1 数据库是什么 将大量数据保存起来,通过计算机加工而成的可以进行高效访问的数据集合称为数据库(Database, DB). 法则 用来管理数据库的计算机系统称为数据库管理系统 ...
- CentOS 6, 编译安装lamp (php-fpm)
1 整体要求 php-fpm.httpd.mysql三者分别安装在三台虚拟机上: 第一台虚拟主机用于安装Mariadb,第二台虚拟主机安装php-fpm:第三台虚拟主机安装httpd.三台主机安装完之 ...
- CSS+DIV命名
原地址:http://www.cnblogs.com/hylaz/archive/2012/10/27/2742743.html#2521377 页头:header 登录条:loginBar 标志:l ...
- Data of Ch5 --Dual rotor
* Results *Conclusion*- little effect of rear rotor on Cp_1- Cp1 is independent of TI** TI effect on ...
- LeetCode(55)Jump Game
题目 Given an array of non-negative integers, you are initially positioned at the first index of the a ...
- 配置bean
[bean配置] 在XML文件中通过bean节点来配置bean <!-- 配置bean class: bean的全类名,通过反射的方式在IOC容器中创建bean,所以要求bean中必须有无参的构 ...
- Automation 的 Wait 工具
public static WebDriverWait createWait(WebDriver driver) { return new WebDriverWait(driver, Environm ...