Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

調整 Android 系統 ProgressDialog 文字大小的方法

編輯:關於 Android 程式設計

系統自帶的 ProgressDialog 文字預設偏小,若想放大或縮小,最簡單的方式就是為它指定一個自訂的 style。以下示範完整的做法。

Activity 程式碼

package com.example.ttt;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.Window;

/**
 * Demo 描述: 改變系統自帶 ProgressDialog 的文字大小
 * 改變方式: 為 ProgressDialog 設置一個 style 即可,參見 styles.xml
 */
public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 為此 ProgressDialog 指定 style
        ProgressDialog dialog = new ProgressDialog(this, R.style.dialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setMessage("loading");
        dialog.setCancelable(false);
        dialog.show();
    }
}

styles.xml(自訂樣式)

關鍵在於最後一行的 android:textSize,把它設為想要的字級即可:

<resources xmlns:android="https://schemas.android.com/apk/res/android">
    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <style name="dialog" parent="@android:style/Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowIsTranslucent">true</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:textSize">25sp</item>
    </style>
</resources>

版面配置

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

透過自訂 style 覆寫 textSize,就能輕鬆控制 ProgressDialog 的提示文字大小。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved