TextView一些技巧

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"  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值