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. 只用 ...
随机推荐
- vs2010中的反编译
有这样的需求,一个.dll文件,如何查看里面的代码呢?网上有很多关于反编译的运用的. http://blog.csdn.net/lyflcear/article/details/8102057 昨天( ...
- JS对Json对象Distinct
Json对象去重 今日有一个需求如下: 从数据库中取出数据源转化成json字符串绑定到隐藏域中,取出的json字符串如下: string data="[{"CompanyName& ...
- Teradata 的rank() 和 row_number() 函数
Teradata数据库中也有和oracle类似的分析函数,功能基本一样.示例如下: RANK() 函数 SELECT * FROM salestbl ORDER BY 1,2; storeid p ...
- vs2010的11个调试技巧和方法
调试是软件开发周期中很重要的一部分.它具有挑战性,同时也很让人疑惑和烦恼.总的来说,对于稍大一点的程序,调试是不可避免的.最近几年,调试工具的发展让很多调试任务变的越来越简单和省时. 这篇文章总结了可 ...
- iOS常见问题(5)
一.注意将之前storyboard中控制器删除之后,拖入一个新的控制器的时候,stroyboard中控制器的class也要重新填入自己想要展示的控制器,告诉stroyboard去加载哪个控制器. 二. ...
- STL学习一:标准模板库理论基础
STL(Standard Template Library,标准模板库)是惠普实验室开发的一系列软件的统称.现然主要出现在C++中,但在被引入C++之前该技术就已经存在了很长的一段时间. STL的从广 ...
- ubuntu12.04之后该死的文件关联
这是抱怨贴~~~当然也提供了解决方法~敬请期待. 这个帖子中我们介绍如何在ubuntu下安装quartus II 13.1,安装完毕功能正常,但是有个“小问题”就是如何在文件管理器中(这里是nauti ...
- C++中使用心得
1.struct成员默认访问方式是public,而 class默认访问方式是private! 2.exit函数终止程序执行会调用析构函数 ,abort函数终止程序不会调用析构函数! 3.静态局部变量直 ...
- svn:Repository UUID 'XXX' doesn't match expected UUID 'YYY'
About a month ago, CodePlex have upgraded their TFS servers to to TFS 2010. While this transition wa ...
- android 自定义ratingbar 图片显示不全的解决方案
在res/style中自定义评分条: <!-- 自定义评分条 --> <style name="roomRatingBar" parent="@andr ...