How to add a line break in an android textView?
此示例演示了如何在android textView中添加换行符。
步骤1? 在Android Studio中创建一个新项目,转到文件? 新建项目,并填写所有必需的详细信息以创建一个新项目。
第2步? 将以下代码添加到res / layout / activity_main.xml。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?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" tools:context=".MainActivity"> <TextView android:id="@+id/textView" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="24sp" android:text="@string/Questions" /> </RelativeLayout> |
第三步 将以下代码添加到src / MainActivity.java中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; public class MainActivity extends AppCompatActivity { TextView textView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.textView); textView.setTextColor(Color.RED); } } |
第4步–打开strings.xml并添加以下代码-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <resources> <string name="app_name">Sample</string> <string name="Questions"> 1. What is your Favourite food? </br> 2. Which is your favourite sport? </br> 3. Whats your bucket list? </br> </string> </resources> |
第5步-将以下代码添加到androidManifest.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="app.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"> <intent-filter> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> |
让我们尝试运行您的应用程序。 我假设您已将实际的Android Mobile设备与计算机连接。 要从android studio运行该应用,请打开您项目的活动文件之一,然后从工具栏中单击运行 icon。 选择您的移动设备作为选项,然后检查将显示默认屏幕的移动设备–
单击此处下载项目代码。