提高Android TextView顯示效果的小技巧

一、設置字體

Android系統默認提供了幾種字體,可以通過以下方式設置。首先在res/font下新建字體文件,如myfont.ttf,然後在xml布局文件中使用,如下所示:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="@font/myfont"
    android:text="Hello World!" />

此外,還可以通過setTypeface方法設置字體,如下所示:

TextView textView=findViewById(R.id.textview);
Typeface typeface=Typeface.createFromAsset(getAssets(),"myfont.ttf");
textView.setTypeface(typeface);

二、設置文字粗細和斜體

通過android:textStyle屬性可以設置粗細或斜體,具體使用方法如下:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textStyle="bold" />

也可以組合使用,設置為粗體和斜體:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textStyle="bold|italic" />

同樣也可以通過代碼設置,如下所示:

TextView textView=findViewById(R.id.textview);
textView.setTypeface(null,Typeface.BOLD_ITALIC);

三、設置文字大小和顏色

通過android:textSize屬性可以設置字體大小,如下所示:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="24sp" />

同時可以通過android:textColor設置文字顏色,如下所示:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="24sp"
    android:textColor="#FF0000" />

通過代碼設置也非常簡單,如下所示:

TextView textView=findViewById(R.id.textview);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,24);
textView.setTextColor(Color.RED);

四、設置文字陰影

通過android:shadowColor、android:shadowDx、android:shadowDy和android:shadowRadius四個屬性可以設置文字的陰影效果,如下所示:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="24sp"
    android:shadowColor="#999999"
    android:shadowDx="2"
    android:shadowDy="2"
    android:shadowRadius="2" />

其中android:shadowColor屬性設置陰影顏色,android:shadowDx和android:shadowDy分別設置陰影在x軸和y軸的偏移量,android:shadowRadius設置陰影半徑。

同樣也可以通過代碼設置,如下所示:

TextView textView=findViewById(R.id.textview);
textView.setShadowLayer(2,2,2,Color.GRAY);

五、設置文字行間距和字間距

通過android:lineSpacingExtra和android:letterSpacing兩個屬性可以設置文字行間距和字間距,如下所示:

<TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:textSize="24sp"
    android:lineSpacingExtra="10dp"
    android:letterSpacing="0.1" />

其中android:lineSpacingExtra屬性設置行間距,單位是dp,android:letterSpacing屬性設置字間距,值為0~1之間的浮點數。

同樣也可以通過代碼設置,如下所示:

TextView textView=findViewById(R.id.textview);
textView.setLineSpacing(10,1);
textView.setLetterSpacing(0.1f);

完整代碼示例

res/font/myfont.ttf

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <font-family
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:fontProviderAuthority="@string/font_provider_authority"
        android:fontProviderCerts="@array/certs"
        android:fontProviderPackage="@string/font_provider_package"
        android:fontProviderQuery="@string/font_provider_query">
        <font
            android:fontStyle="normal"
            android:fontWeight="400"
            android:font="@font/myfont" />
    </font-family>
</resources>

res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:fontFamily="@font/myfont"
        android:letterSpacing="0.1"
        android:lineSpacingExtra="10dp"
        android:shadowColor="#999999"
        android:shadowDx="2"
        android:shadowDy="2"
        android:shadowRadius="2"
        android:text="Hello World!"
        android:textColor="#FF0000"
        android:textSize="24sp"
        android:textStyle="bold|italic"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.5" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatActivity;

import android.util.TypedValue;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //設置字體
        TextView textView1=findViewById(R.id.textview);
        Typeface typeface=Typeface.createFromAsset(getAssets(),"myfont.ttf");
        textView1.setTypeface(typeface);

        //設置文字粗細和斜體
        TextView textView2=findViewById(R.id.textview);
        textView2.setTypeface(null,Typeface.BOLD_ITALIC);

        //設置文字大小和顏色
        TextView textView3=findViewById(R.id.textview);
        textView3.setTextSize(TypedValue.COMPLEX_UNIT_SP,24);
        textView3.setTextColor(Color.RED);

        //設置文字陰影
        TextView textView4=findViewById(R.id.textview);
        textView4.setShadowLayer(2,2,2,Color.GRAY);

        //設置文字行間距和字間距
        TextView textView5=findViewById(R.id.textview);
        textView5.setLineSpacing(10,1);
        textView5.setLetterSpacing(0.1f);
    }
}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/270727.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-16 13:38
下一篇 2024-12-16 13:38

相關推薦

  • Android ViewPager和ScrollView滑動衝突問題

    Android開發中,ViewPager和ScrollView是兩個常用的控制項。但是當它們同時使用時,可能會發生滑動衝突的問題。本文將從多個方面介紹解決Android ViewPa…

    編程 2025-04-28
  • Android如何點擊其他區域收起軟鍵盤

    在Android應用中,當輸入框獲取焦點彈出軟鍵盤後,我們希望能夠點擊其他區域使軟鍵盤消失,以提升用戶體驗。本篇文章將說明如何實現這一功能。 一、獲取焦點並顯示軟鍵盤 在Andro…

    編程 2025-04-28
  • Android Studio HUD 實現指南

    本文將會以實例來詳細闡述如何在 Android Studio 中使用 HUD 功能實現菊花等待指示器的效果。 一、引入依賴庫 首先,我們需要在 build.gradle 文件中引入…

    編程 2025-04-27
  • Android和Vue3混合開發方案

    本文將介紹如何將Android和Vue3結合起來進行混合開發,以及其中的優勢和注意事項。 一、環境搭建 在進行混合開發之前,需要搭建好相應的開發環境。首先需要安裝 Android …

    編程 2025-04-27
  • Android Java Utils 可以如何提高你的開發效率

    Android Java Utils 是一款提供了一系列方便實用的工具類的 Java 庫,可以幫助開發者更加高效地進行 Android 開發,提高開發效率。本文將從以下幾個方面對 …

    編程 2025-04-27
  • Android JUnit測試完成程序自動退出決方法

    對於一些Android JUnit測試的開發人員來說,程序自動退出是一個經常面臨的困擾。下面從多個方面給出解決方法。 一、檢查測試代碼 首先,我們應該仔細檢查我們的測試代碼,確保它…

    編程 2025-04-25
  • Android Activity啟動流程

    一、Activity概述 Android應用程序是由許多Activity組成的。一個Activity代表一個屏幕上的窗口。用戶與應用程序交互時,Activity會接收用戶的輸入並處…

    編程 2025-04-25
  • Android單元測試詳解

    一、單元測試概述 單元測試是指對軟體中的最小可測試單元進行檢查和驗證。在Android開發中,單元測試是非常重要的一環,可以保證代碼的質量、穩定性以及可維護性。 在Android開…

    編程 2025-04-25
  • Android WebView載入本地HTML

    一、介紹 Android WebView是一個內置的瀏覽器,它允許開發人員在應用中嵌入網頁。使用WebView可以輕鬆地在應用程序中顯示本地或遠程的HTML內容。本篇文章將重點講述…

    編程 2025-04-24
  • Android Wakelock詳解

    一、什麼是Android Wakelock? 在Android應用開發中,Wakelock被廣泛應用於保持屏幕或CPU處於喚醒狀態,以便應用程序可以繼續執行後台任務,直到任務完成。…

    編程 2025-04-24

發表回復

登錄後才能評論