Android设置TextView显示一行或多行
来源:互联网 发布:雀巢中国总部 知乎 编辑:程序博客网 时间:2024/06/02 03:26
在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="2" 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;@Overridepublic void onClick(View arg0) {// TODO Auto-generated method stubif (flag) {flag = false;text.setEllipsize(null);// 展开text.setSingleLine(flag);button.setText("隐藏");} else {flag = true;text.setMaxLines(2);// 收缩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>
<span style="font-size:14px;">viewItme.item_btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubBoolean 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(10);viewItme.item_btn.setBackgroundResource(R.drawable.unfloddd);} else {// 收缩viewItme.item_btn.setTag("true");viewItme.item_describe.setMaxLines(1);viewItme.item_btn.setBackgroundResource(R.drawable.packup);}}});</span>
2 0
- Android设置TextView显示一行或多行
- android textview一行显示
- 解决Android中TextView多行显示的最后一行被遮住一半的bug
- android TextView一行只显示一个文字
- Android 一行显示全部内容的TextView--SingleLineTextView
- Android textview 多行显示
- Android textview显示多行
- TextView设置一行最多显示6个字是什么属性?
- 设置TextView 一行文本显示不同的效果
- textview 设置text 一行显示不同颜色引入 Html
- TextView设置一行最多显示5个字是什么属性
- TextView设置一行最多显示5个字是什么属性
- TextView自动适应一行显示和设置间距
- android textview 显示一行,且超出自动截断,显示"..."
- android textview 显示一行,且超出自动截断,显示"..."
- android textview 显示一行,且超出自动截断,显示"..."
- Android textview 只显示一行,多余部分显示"..."
- android中TextView同一行显示不同颜色文字
- [web安全] web安全小知识点汇总
- oracle解除表锁定
- SecureCRT乱码解决
- 数据存取之Preference浅析
- XCode的一些调试技巧
- Android设置TextView显示一行或多行
- Django开发博客(四)——建立文章列表
- android sqlite 应用
- 学习之随堂笔记——屏幕适配
- MyBits学习小知识及错误二
- Oracle11g 账号锁定及密码有效期问题
- 开源控件学习之-侧滑listview进行删除(一)(自定义listview)
- 数据库学习
- solr定时实时重建索引和增量更新