一、效果图
二、案例演示
1、新建安卓应用
基于Empty Activity
模板创建 - TextViewDemo
2、自定义边框配置文件
点击【ok】按钮
<?xml version="1.0" encoding="utf-8"?>
< shape xmlns: android= " http://schemas.android.com/apk/res/android"
android: shape= " rectangle" >
< corners android: radius= " 5dp" />
< solid android: color= " #dddddd" />
< stroke
android: width= " 1dp"
android: color= " #555555" />
< padding
android: bottom= " 10dp"
android: left= " 10dp"
android: right= " 10dp"
android: top= " 10dp" />
< gradient
android: centerColor= " #ffff00"
android: endColor= " #00ff00"
android: startColor= " #aaaaaa" />
</ shape>
3、字符串资源文件
strings.xml
< resources>
< string name = " app_name" > 标签演示</ string>
< string name = " normal" > 常规:Welcome to Android World</ string>
< string name = " bold" > 粗体:Welcome to Android World</ string>
< string name = " italic" > 斜体:Welcome to Android World</ string>
< string name = " bold_italic" > 粗斜:Welcome to Android World</ string>
< string name = " all_caps" > 全大写:Welcome to Android World</ string>
< string name = " theorem" > 勾股定理:</ string>
< string name = " equation" > 化学方程式:</ string>
< string name = " android" > 安卓</ string>
</ resources>
4、主布局资源文件
<?xml version="1.0" encoding="utf-8"?>
< LinearLayout 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: orientation= " vertical"
android: background= " #000000"
android: padding= " 10dp"
tools: context= " .MainActivity" >
< TextView
android: id= " @+id/tvn_normal"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: text= " @string/normal"
android: textColor= " @android:color/holo_blue_bright"
android: textSize= " 15sp"
android: textStyle= " normal" />
< TextView
android: id= " @+id/tv_bold"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: text= " @string/bold"
android: textColor= " @android:color/holo_green_light"
android: textSize= " 15sp"
android: textStyle= " bold" />
< TextView
android: id= " @+id/tv_italic"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: text= " @string/italic"
android: textColor= " @android:color/holo_orange_light"
android: textSize= " 15sp"
android: textStyle= " italic" />
< TextView
android: id= " @+id/tv_boldItalic"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: text= " @string/bold_italic"
android: textColor= " @android:color/holo_purple"
android: textSize= " 15sp"
android: textStyle= " bold|italic" />
< View
android: layout_width= " match_parent"
android: layout_height= " 1dp"
android: layout_marginTop= " 5dp"
android: layout_marginBottom= " 5dp"
android: background= " #aaaaaa" />
< TextView
android: id= " @+id/tv_allCaps"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: text= " @string/all_caps"
android: textAllCaps= " true"
android: textColor= " #ffffff"
android: textSize= " 15sp"
android: textStyle= " normal" />
< TextView
android: id= " @+id/tv_theorem"
android: layout_width= " wrap_content"
android: layout_height= " 40dp"
android: gravity= " center_vertical"
android: layout_marginTop= " 10dp"
android: text= " @string/theorem"
android: textColor= " @android:color/holo_red_light"
android: textSize= " 20sp" />
< TextView
android: id= " @+id/tv_equation"
android: layout_width= " wrap_content"
android: layout_height= " 40dp"
android: layout_marginTop= " 10dp"
android: gravity= " center_vertical"
android: text= " @string/equation"
android: textColor= " @android:color/holo_green_dark"
android: textSize= " 20sp" />
< View
android: layout_width= " match_parent"
android: layout_height= " 1dp"
android: layout_marginTop= " 5dp"
android: layout_marginBottom= " 5dp"
android: background= " #aaaaaa" />
< TextView
android: id= " @+id/tv_web"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_marginTop= " 10dp"
android: autoLink= " web"
android: text= " 网址:http://www.baidu.com"
android: textColor= " @android:color/holo_orange_light"
android: textSize= " 15sp" />
< TextView
android: id= " @+id/tv_email"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_marginTop= " 10dp"
android: autoLink= " email"
android: text= " 邮箱:maths007@163.com"
android: textColor= " @android:color/holo_green_light"
android: textSize= " 15sp" />
< TextView
android: id= " @+id/tv_phone"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_marginTop= " 10dp"
android: autoLink= " phone"
android: text= " 电话:19161576451"
android: textColor= " @android:color/holo_red_light"
android: textSize= " 15sp" />
< View
android: layout_width= " match_parent"
android: layout_height= " 1dp"
android: layout_marginTop= " 5dp"
android: layout_marginBottom= " 5dp"
android: background= " #aaaaaa" />
< TextView
android: id= " @+id/tv_borderedText"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_marginTop= " 10dp"
android: background= " @drawable/custom_border"
android: text= " 带边框的文本:永不放弃的梦想"
android: textColor= " #ff0000"
android: textSize= " 15sp" />
< LinearLayout
android: layout_width= " match_parent"
android: layout_height= " 160dp"
android: orientation= " horizontal"
android: gravity= " center" >
< TextView
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: drawableLeft= " @mipmap/ic_launcher"
android: gravity= " center"
android: text= " @string/android"
android: textColor= " #ffffff"
android: textSize= " 18sp" />
< TextView
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: drawableTop= " @mipmap/ic_launcher"
android: gravity= " center"
android: text= " @string/android"
android: textColor= " #ffffff"
android: textSize= " 18sp" />
< TextView
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: drawableBottom= " @mipmap/ic_launcher"
android: gravity= " center"
android: text= " @string/android"
android: textColor= " #ffffff"
android: textSize= " 18sp" />
< TextView
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: drawableRight= " @mipmap/ic_launcher"
android: gravity= " center"
android: text= " @string/android"
android: textColor= " #ffffff"
android: textSize= " 18sp" />
</ LinearLayout>
</ LinearLayout>
5、主界面类
MainActivity
package net. hxl. text_view_demo ;
import androidx. appcompat. app. AppCompatActivity ;
import android. os. Bundle ;
import android. text. Html ;
import android. widget. TextView ;
public class MainActivity extends AppCompatActivity {
private TextView tvTheorem;
private TextView tvEquation;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
tvTheorem = findViewById ( R . id. tv_theorem) ;
tvEquation = findViewById ( R . id. tv_equation) ;
tvTheorem. setText ( Html . fromHtml ( "显示勾股定理:a<sup>2</sup> + b<sup>2</sup> = c<sup>2</sup>" ) ) ;
tvEquation. setText ( Html . fromHtml ( "化学方程式:2H<sub>2</sub> + O<sub>2</sub> = 2H<sub>2</sub>O" ) ) ;
}
}
6、启动应用,查看效果