TextView一些技巧

来源:互联网 发布:js分页代码实例 编辑:程序博客网 时间:2024/06/08 15:35

1.Android中如何让TextView显示指定的行数并且多出的部分显示省略号

大家都知道,如果要让TextView只显示一行,多出部分用省略号代替的话非常容易,只需要为xml文件中的TextView控件添加Android:singleLine="true"即可,那么如果我想让它显示多行的时候应该怎么做呢?其实也很简单,只要添加以下两个属性就可以了:

android:lines="2"
android:ellipsize="end"

这样就可以让TextView最多显示2行,如果要显示指定的其它行数,只要修改上面的android:lines属性即可,不过这里有个不好的地方,比如你设置了最多显示两行,如果内容用一行就可以全部显示了,这时候TextView还是会占用两行的高度空间。

如果不想让TextView占用固定的高度,可以改用android:maxLines="2"这个属性来解决

2.TextView实现一行居中显示,多行居左显示。

<?xml version="1.0" encoding="utf-8"?><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"    >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerInParent="true">        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Hello World!" />    </LinearLayout></RelativeLayout>
3.

TextView的android:maxHeight,android:minHeight的正确设置


设置最大(最小)高度(宽度)时,

需要同时设置Android:adjustViewBounds="true",这样设置才会生效。

在代码中设置时,需要setAdjustViewBounds为true。一个layout的实例:

android:adjustViewBounds="true"  

android:maxHeight="150.0dip"  

android:maxWidth="150.0dip"  

android:minHeight="33.0dip"  

android:minWidth="48.0dip"  

原创粉丝点击