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. 只用 ...
随机推荐
- cadence PCB绘制步骤
1 创建一个PCB文件 file -> new 2 创建一个板框 add -> line ,在 options 选型中选择好,板框为 长 4400mil 宽 3200 3 给PCB板框 ...
- 关于一道简单的Java 基础面试题的剖析: short s1=1;s1 = s1 +1会报错吗?
package common; public class ShortTypeTest { /* * @param args */ public static void main(String[] ar ...
- Ubuntu下Code::Blocks无法编译 /bin/sh: 1: g++ not found 解决办法
Linux下Code::Blocks无法编译运行提示 /bin/sh: 1: g++ not found 的解决办法 今天在Ubuntu 12.04 软件中心中选装了Code::Blocks,安装完成 ...
- 最火的.NET开源项目(转)
综合类 微软企业库 微软官方出品,是为了协助开发商解决企业级应用开发过程中所面临的一系列共性的问题, 如安全(Security).日志(Logging).数据访问(Data Access).配置管理( ...
- 如何在eclipse中添加android ADT
百度经验:http://jingyan.baidu.com/article/b0b63dbfa9e0a74a4830701e.html 截图:
- R语言学习笔记 之 可视化地研究参议员相似性
基于相似性聚类 很多时候,我们想了解一群人中的一个成员与其他成员之间有多么相似.例如,假设我们是一家品牌营销公司,刚刚完成了一份有潜力新品牌的研究调查问卷.在这份调查问卷中,我们向一群人展示了新品牌的 ...
- 推荐acm题目
杭电 http://acm.hdu.edu.cn/onlineuser.php. 浙大 http://acm.zju.edu.cn/onlinejudge/submit.do?problemId= ...
- 在windows 7搭建xcode开发环境
前言:本文适用于没有ios开发环境(如无iphone,mac等),在windows上搭建出ios的开发环境.值得注意的是,在windows搭建的ios开发环境只能做开发测试,无法发布产品.若需要发布产 ...
- cmd下windows批处理,获取当前系统时间,生成日志文件名
示例: rdGetRTData_log%date:~0,4%%date:~5,2%%date:~8,2%.txt 生成格式: rdGetRTData_log20151103.txt 编写Windows ...
- Matlab中mat2cell的使用
怎样用mat2cell将一个100*100的矩阵分成10个10*100的矩阵? 根据帮助中 c = mat2cell(x,m,n)应该这样写 mat2cell(x,[10 10 10 10 10 10 ...