常见控件之TextView

本文探讨了Android中TextView的行间距设置,包括如何使用lineSpacingExtra增加额外行间距,以及通过lineSpacingMultiplier设置行间距为字体高度的倍数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.常见属性设置

lineSpacingExtra :行间距   

也可以使用 android:lineSpacingMultiplier="2" 行间距相当于字体高的两倍


singleLine="true"  只显示一行

组合使用:
android:singleLine="true"
android:ellipsize="middle"
如果一行显示不下,则出现的"..."出现在中间,默认是出现在最后边

android:ellipsize="middle|marquee"
marquee : 跑马灯的意思

但是注意,要想让TextView里面的文字跑起来还要设置:
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
并且在代码中加上: findViewById(R.id.textView).requestFocus();

android:maxLines="2"  最多显示多少行
android:lines="4"  不管多少字,始终显示4行
android:fontFamily="monospace" 设置字体

android:textColor="#0f0"  字体颜色
android:textSize="18sp"  字体大小
android:textStyle="bold|italic|normal"  字体样式有:粗体,斜体,正常


2.加载自己的字体:
i).将字体文件放到 assets 目录下
ii).代码中写
   TextView textView = (TextView)findViewById(R.id.textview1);
   TypeFace typeFace = TypeFace.createFromAsset(getAssets(),"font.ttf");
   textView.setTypeface(typeFace);


3.给TextView 设置图标
drawablePadding  文本句图标间距
drawableXX  设置四个方向图标

android:drawableLeft="@drawable/logo"
android:drawableRight
android:drawableTop
android:drawableBottom

注意:android:drawablePadding  是指 图标和文本的间距
     android:padding  可以设置图标和TextView的内间距

4. 图文混排效果
有很多的span可以设置,这里列举一些常见的
TextView textView = (TextView)findViewById(R.id.textview);
SpannableString ss = new SpannableString(getResources().getString(R.string.));
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),gR.drawable.xx);
ss.setSpan(new ImageSpan(this,bitmap),3,4,Spanned.SPAN_EXCLUSIVE_);

ss.setSpan(new UnderLineSpan(),8,15,Spanned.SPAN_EXCLUSIVE_EXCLUSI);

ss.setSpan(new ForegroundColorSpan(Color.WHITE),20,25,);
ss.setSpan(new BackgroundColorSpan(),,,);

ss.setSpan(new ClickableSpan(){
     @Override
     pubic void onClick(View widget){
          Toast.makeText().show()
     }
},30,38,Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
注:要使这个click事件生效,必须加上:
textView.setMovementMethod(LinkMovementMethod.getInstance());


ss.setSpan(new AbsoluteSizeSpan(30,true),42,46,);
ss.setSpan(new RelativeSizeSpan(1.5f),,);


ss.setSpan(new StyleSpan(TypeFace.BOLD|TypeFace.ITALIC),,)


ss.setSpan(new StrikethroughSpan(),,);//文字之间加一条线


ss.setSpan(new TypefaceSpan("monospace"),,); //字体


ss.setSpan(new TextAppearanceSpan(this,R.style.myTextStyle),,);
注:这个样式是定义在styles.xml
<resources>
     <style name="myTextStyle" parent="@android:style/TextAppearence">
          <item name="android:textColor">#00f</item>
          <item name="android:textSize">20dp</item>
          <item name="android:textStyle">italic</item>
     </style>
</resources>


如何实现自己的Span:
class MySpan extends ForegroundColorSpan{
     public MySpan(int color){
          super(color);
     }


     @Override
     public void updateDrawState(TextPaint ds){
          super.updateDrawState(ds);
          ds.setUnderlineText(true);
     }
}


ss.setSpan(new MySpan(Color.YELLOW),,);


textView.setText(ss);


其他的混排方式:
1)设置 AutoLink 
  <TextView
     android:autoLink="all"  //email,map,none,phone,web
     >  
     系统就会自动去解析它,然后点击后会自动打开可以调用它的app


2)Html.fromHtml
  假如TextView 设置的文本为 html文本,即 <font ..>xxx</font>
  则在代码中需要添加:
  textView.setText(Html.fromHtml("<font ..>xx</font>"));
  textView.setMovementMethod(LinkMovementMethod.getInstance());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值