如何在 Android 布局 XML 文件中定义下划线文本?
此示例说明如何在 Android 布局 xml 文件中定义下划线文本。
步骤 1 − 在 Android Studio 中创建新项目,转至文件 ⇒ 新项目,并填写所有必需信息以创建新项目。
步骤 2 − 将以下代码添加到 res/layout/activity_main.java
<? 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" android :orientation= "vertical" tools :context= ".MainActivity" > <TextView android :id= "@+id/textView" android :layout_width= "match_parent" android :layout_height= "wrap_content" android :layout_centerInParent= "true" android :gravity= "center" android :padding= "16dp" android :text= "Bottom of the screen" android :textAppearance= "@style/Base.TextAppearance.AppCompat.Large" /> </RelativeLayout>
步骤 3 − 将以下代码添加到 src/MainActivity.java
package app.tutorialspoint.com.sample ;
import android.os.Bundle ;
import android.support.v7.app.AppCompatActivity ;
import android.text.SpannableString ;
import android.text.style.UnderlineSpan ;
import android.widget.TextView ;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super .onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main ) ;
TextView textView = findViewById(R.id. textView ) ;
SpannableString content = new SpannableString( "Content" ) ;
content.setSpan( new UnderlineSpan() , 0 , content.length() , 0 ) ;
textView.setText(content) ;
}
}步骤 4 − 将以下代码添加到 androidManifest.xml
<? xml version= "1.0" encoding= "utf-8" ?> <manifest xmlns: android = "http://schemas.android.com/apk/res/android" package= "app.tutorialspoint.com.sample" > <application android :allowBackup= "true" android :icon= "@mipmap/ic_launcher" android:label= "@string/app_name" android:roundIcon= "@mipmap/ic_launcher_round" android:supportsRtl= "true" android:theme= "@style/AppTheme" > <activity android:name= ".MainActivity" > <intent-filter> <action android:name= "android.intent.action.MAIN" /> <category android:name= "android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
让我们尝试运行你的应用程序。我假设你已将你的安卓移动设备与电脑连接。要从 android studio 运行应用,请打开你的一个项目活动文件并单击运行
工具栏中的图标。选择你的移动设备,然后查看你将显示默认屏幕的移动设备 -

广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP