失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > TextView字体 行距 html格式 超链接 最大长度的设定

TextView字体 行距 html格式 超链接 最大长度的设定

时间:2021-11-06 17:25:21

相关推荐

TextView字体 行距 html格式 超链接 最大长度的设定

颜色,大小

<!-- 设置字体的大小,推荐用sp做单位;字体颜色以#开头 --> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:textColor="#0000ff" android:textSize="16sp" />

行间距

<!-- android:lineSpacingExtra="8dp" 设定行距 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="万众瞩目的北京奥运会吉祥物于北京时间11日20:18正式揭晓,奥运吉祥物福娃:

形象为鱼、熊猫、奥运圣火、藏羚羊、燕子,名字是贝贝、晶晶、欢欢、迎迎、妮妮,即北京欢迎你" android:textColor="#0000ff" android:lineSpacingExtra="8dp" android:layout_marginTop="16dp" android:textSize="16sp" />

内部特殊文字识别,识别电话、邮箱等

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:autoLink="web" android:text="Google一下: .hk" />

设置最多能显示多少文字

<!-- android:maxLength="7" 设置显示文字的最大的长度 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:maxLength="7" android:text="1234567890" />

设定文字样式

android:textStyle="bold" 表示文字是粗体

<!-- android:textStyle="italic" 设定字体样式 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你好,android" android:textColor="#000000" android:layout_marginTop="16dp" android:textStyle="italic" />

用样式文件来设定字体

<!-- style="@style/text_style" 用独立的样式文件作为字体样式,直接用style属性即可 --> <TextView android:text="你好,android" android:layout_marginTop="16dp" style="@style/text_style" />

这里用到的style文件

<style name="text_style"> <item name="android:textSize">20sp</item> <item name="android:textColor">#ff0000</item> <item name="android:textStyle">italic|bold</item> <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> </style>

设置文字与图片的位置

<!-- android:drawableLeft="@drawable/ic_launcher" 设定文字与图片的位置,上下左右都行 --> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:drawableLeft="@drawable/ic_launcher" android:layout_marginTop="16dp" android:text="左边是图片"/>

文字过长时显示的效果

<!-- ellipsize="end" 设定文字过长时的显示效果 --> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:singleLine="true" android:ellipsize="end" android:text="设置当文字过长时,该控件该如何显示。可设置如下属性值:start省略号显示在开头;

end省略号显示在结尾; middle省略号显示在中间;

marquee以跑马灯的方式显示(动画横向移动)"/>

通过代码进行文字的设定

package com.kale.textview; import android.app.Activity; import android.graphics.Typeface; import android.os.Bundle; import android.text.Html; import android.text.method.LinkMovementMethod; import android.widget.TextView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView)findViewById(R.id.textView1); tv.getPaint().setFakeBoldText(true);//设置文字为粗体 Typeface typeFace =Typeface.createFromAsset(getAssets(),"fonts/mini.ttf");//加载自定义字体tv.setTypeface(typeFace); String html_marquee = "万众瞩目的北京奥运会<a href = ''>吉祥物</a>" + //超链接“吉祥物”字段到百度 "于北京时间11日20:18正式揭晓," + "奥运吉祥物福娃:形象为鱼、熊猫、奥运圣火、藏羚羊、燕子," + "名字是贝贝、晶晶、欢欢、迎迎、妮妮,即北京欢迎你 电话:15667856218"; CharSequence charSequence_marquee = Html.fromHtml(html_marquee); tv.setText(charSequence_marquee);//设定textView显示的文字 tv.setMovementMethod(LinkMovementMethod.getInstance()); //点击时产生超链接效果,补写的话点击无效} }

源码下载:/detail/shark0017/7583515

如果觉得《TextView字体 行距 html格式 超链接 最大长度的设定》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。