Android设置TextView显示一行或多行
在listView的item中或者是特殊的业务需求中,会要求TextView的内容不完全显示,只有通过一个指定的操作后才显示所有的,比如说一个按钮或者是其它的什么控件。
要想实现这个效果并不难,只要控制好TextView的行数就行。文章中介绍了两种实现方法,一种是给button添加Flag,另一种是给button添加Tag,两种方法都可以,具体说不上哪种更好,哪种适合用哪种。
第一种方法的布局,注意TextView中必须加上android:ellipsize和android:maxLines这两条属性,不然的话效果出不来:
<span style="font-size:14px;"><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="200dp"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines=""
android:text="四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉四谛法邝健廉" /> <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></span>
接下来就可以在类中控制这个TextView显示或者隐藏了。
<span style="font-size:14px;"> button.setOnClickListener(new OnClickListener() {
Boolean flag = true;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (flag) {
flag = false;
text.setEllipsize(null);// 展开
text.setSingleLine(flag);
button.setText("隐藏");
} else {
flag = true;
text.setMaxLines();// 收缩
button.setText("显示");
// text.setEllipsize(TruncateAt.END);
}
}
});</span>
或者可以在Button中添加一个Tag
<span style="font-size:14px;"><Button
android:id="@+id/item_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="@drawable/packup"
android:tag="true" /></span>
同样,在代码中获取到改按钮的tag进行控制
<span style="font-size:14px;"> viewItme.item_btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Boolean flag = Boolean.valueOf((String) viewItme.item_btn.getTag()) ;
if (flag) {
// 展开
viewItme.item_btn.setTag("false");
viewItme.inspecttext_tv.setEllipsize(null);
viewItme.inspecttype_tv.setEllipsize(null);
viewItme.item_describe.setEllipsize(null);
viewItme.item_describe.setMaxLines();
viewItme.item_btn.setBackgroundResource(R.drawable.unfloddd);
} else {
// 收缩
viewItme.item_btn.setTag("true");
viewItme.item_describe.setMaxLines();
viewItme.item_btn.setBackgroundResource(R.drawable.packup);
}
}
});</span>
Android设置TextView显示一行或多行的更多相关文章
- android textview 显示一行,且超出自动截断,显示"..."
android textview 显示一行,且超出自动截断,显示"..." <TextView android:layout_width="wrap_content ...
- Android设置TextView行间距(非行高)
Android设置TextView行间距(非行高) Android系统中TextView默认显示中文时会比较紧凑,不是很美观. 为了让每行保持一定的行间距,可以设置属性android:lineSpac ...
- 设置TextView显示的文字可以复制
设置TextView显示的文字可以复制 效果图 在xml中设置 <TextView android:layout_width="wrap_content" android:l ...
- Android 设置TextView字体颜色
设置TextView字体的颜色其实很简单,尤其是直接在XML文件中,可以直接通过textColor属性指定颜色值,达到设置文本颜色的效果:那在代码中如何动态设置字体的颜色值呢? 接下来,介绍如何通过J ...
- $Android设置TextView的字体
做项目的时候,需要使用到手写字体来让内容更加的美观.可是程序中默认使用的是系统的默认字体,怎么将TextView(或EditText)的字体设置成自己想要的字体呢?步骤如下: 1.下载字体文件(.tt ...
- Android recyclerview 只显示一行 宽度不适配
最近学习recyclerview 遇到的问题 1.宽度不适配 正确写法 LayoutInflater.from(context).inflate(R.layout.item_view,parent,f ...
- android 设置TextView水平滚动和解决首行缩进问题
android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMod ...
- Android中Textview显示Html,图文混排,支持图片点击放大
本文首发于网易云社区 对于呈现Html文本来说,Android提供的Webview控件可以得到很好的效果,但使用Webview控件的弊端是效率相对比较低,对于呈现简单的html文本的话,杀鸡不必使用牛 ...
- Android中用TextView显示大量文字的方法
最近学习Android中,试着实现一个简单的显示新闻Demo的时候,遇到了一个问题:一条新闻的内容文字很多,放在TextView上面超出屏幕了,怎么破? 查了一下资料,找到了两种方法实现: 1. 只用 ...
随机推荐
- 【面试虐菜】—— Apache知识整理
Apache性能调优1 Apache 部分:1. 移除不用的模块.2. 使用 mod_disk_cache NOT mod_mem_cache .3. 扁平架构配置mod_disk_cache.4. ...
- Windows7鼠标右键里没有新建文本文件的选项,解决办法
1.“开始”->“运行”,输入"regedit",打开注册表编辑器 2.展开HKEY_CLASSES_ROOT,找到.txt 3.选中.txt,查看右侧窗格的“默认值”是不是 ...
- GITHUB 提交错误 Error: Permission denied (publickey) 解决
1. 在开发机上生成自己的密钥 ssh-keygen -b 1024 -t rsa -b 指密钥对长度 -t 指加密方式 Enter file in which to save the key ( ...
- edge.js架起node.js和.net互操作桥梁
今天要介绍的是edge.js这个github上刚兴起的开源项目,它可以让node.js和.net之间在in-process下互操作..net版本在4.5及以上,因为.net4.5带来的Task,asy ...
- JAVA算术运算符、关系运算符和位运算符
算术运算符 1.java的算数运算符包括+(加).-(减).*(乘)./(除).%(取余),在运算过程中出现的隐式转换原则和C语言一样:2. 高位数据向低位数据转化要使用强制转化: 关系运算符 1.j ...
- .net datatable 添加一列
dt.Columns.Add("image", Type.GetType("System.String")); foreach (DataRow dr in d ...
- android 开发高仿QQ表情选择、输入框
首先大家看效果: 用到的文件有(源码文件有,只包含表情.输入框等有关文件,工程项目是公司项目,恕不公开啦): res: drawable/face_del_icon.xml drawable/iv_f ...
- 生成最小树prim算法
最小生成树prim算法实现 ‘ ’最小生成树,就是权值(两点间直线的值)之和的最小值. 首先,要用二维数组记录点和权值.如上图所示无向图: int G[6][6]; G[1] ...
- VisionTimer BUG && Start
void Start() { vp_Timer.In(0.0f, delegate() { Debug.Log("Start"); }, 10, 1.0f); } Version ...
- 谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持
谈谈WCF中的Data Contract(3):WCF Data Contract对Collection & Dictionary的支持 在本篇文章上一部分Order Processing的例 ...