我是android应用程序开发的新手。我想更改应用程序的字体。我只想知道通过XML和Java更改字体的不同方法。要更改以下字体:1.TextView 2.RadioButton 3.EditText 4.CheckBox等。
右键单击资产并创建文件夹字体。
下载.ttf文件,即fontName.ttf并粘贴在fonts文件夹内。
现在您必须做主要的事情。在您的包内创建一个类。
此类用于TextView
public class Railway_Regular extends TextView {
public Railway_Regular(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
Run Code Online (Sandbox Code Playgroud)
此类用于Button
public class Railway_Regular_Btn extends Button {
public Railway_Regular_Btn(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
Run Code Online (Sandbox Code Playgroud)
此类用于EditText
public class Railway_Regular_EdTx extends EditText {
public Railway_Regular_EdTx(Context context, AttributeSet attrs) {
super(context, attrs);
this.setTypeface(Typeface.createFromAsset(context.getAssets(),"fonts/Raleway-Regular.ttf"));
}}
Run Code Online (Sandbox Code Playgroud)
像这些一样,您可以为所有小部件创建类,并将fontname.ttf引用到您的类。
现在,将textview设置为您的字体。
对于TextView
<package_name.fonts.Railway_Regular
android:padding="5sp"
android:id="@+id/test_nameDR"
android:layout_marginTop="4dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Details Text"
android:textColor="@color/black"
android:textSize="14sp"/>
Run Code Online (Sandbox Code Playgroud)
用于按钮
<package_name.fonts.Railway_Regular_Btn
android:id="@+id/revSubmit"
android:background="@color/greenDeep"
android:layout_marginTop="-54dp"
android:layout_width="match_parent"
android:layout_height="54dp"
android:textColor="@color/white"
android:textSize="16dp"
android:text="SUBMIT REVIEW"
/>
Run Code Online (Sandbox Code Playgroud)
用于EditText
<package_name.fonts.Railway_Regular_EdTx
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="15dp"
android:textColorHint="@color/lightgray"
android:textSize="14dp"
android:background="@drawable/edit_text"
android:id="@+id/email_id"
android:hint="Email Id"
android:inputType="textEmailAddress" />
Run Code Online (Sandbox Code Playgroud)
更改字体的第二种方法:
Typeface typeface,typeface2;
typeface = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-Regular.ttf");
typeface2 = Typeface.createFromAsset(this.getAssets(),"fonts/Raleway-SemiBold.ttf");
button1.setTypeface(typeface);
edit_text1.setTypeface(typeface2);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5620 次 |
| 最近记录: |