一、設置字體
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