如何在 Android 上的 TextView 中對齊文字?


本示例演示瞭如何在 Android 上的 TextView 中對齊文字。

步驟 1  − 在 Android Studio 中建立一個新專案,前往 File ⇒ New Project 並填寫所有必需的詳細資訊以建立一個新專案。

步驟 2  − 將以下程式碼新增到 res/layout/activity_main.xml。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   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/tvJustified"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:padding="20sp"
      android:justificationMode="inter_word"
      android:text="The text in this TextView is justified.
      This feature is introduced in Android version >= 8.0.
      The text in this TextView is justified.
      This feature is introduced in Android version >= 8.0.
      The text in this TextView is justified.
      This feature is introduced in Android version >= 8.0." />
   <TextView
      android:id="@+id/tvNotJustified"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textSize="20sp"
      android:padding="20sp"
      android:text="The text in this TextView is not justified.
      No justification. The text in this TextView is not justified.
      No justification. The text in this
      TextView is not justified. No justification. " />
</LinearLayout>

步驟 3  − 將以下程式碼新增到 src/MainActivity.java

package com.example.sample;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }
}

步驟 3  − 將以下程式碼新增到 Manifests/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.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 移動裝置與計算機連線。要從 Android Studio 執行應用,請開啟其中一個專案的活動檔案,然後單擊工具欄上的 Run 播放圖示 圖示。選擇移動裝置作為選項,然後檢查將顯示預設螢幕的移動裝置

單擊 此處 下載專案程式碼。

更新時間: 03-07-2020

6K+ 瀏覽

啟動您的職業生涯

完成課程即可獲得認證

開始
廣告
© . All rights reserved.