簡體   English   中英

如何以編程方式更改整個應用程序中的字體大小,Android?

[英]How to change the Font Size in a whole Application programmatically, Android?

我創建了 Spinner ,其中包含從 "8" 到 "46" 的字體大小列表。 我可以單擊字體大小並在微調器中顯示它。

我的需要是,如果我在 Spinner 中單擊字體大小“26”,那么它應該應用於我的整個項目。 就像應用到我的屏幕、Textview 外觀、Edittext - 粗體/斜體等。同樣,如果我單擊 46 大小,那么它應該應用於我的整個項目。

我怎么能以編程方式做到這一點?

Android文檔並未具體說明通過用戶在應用程序級別進行選擇來全局更改字體大小的最有效方法。

我覺得黑魔回答有問題。

問題在於許多Android小部件都TextViewTextView ,例如ButtonRadioButtonCheckBox 其中一些是TextView間接子TextView ,這使得在這些類中實現TextView自定義版本非常困難。

然而,正如Siddharth Lele 在他的評論中指出的那樣,使用stylesthemes是處理整個應用程序中文本大小變化的更好方法。

我們為布局設置樣式以控制視圖的外觀。 主題本質上只是這些風格的集合。 但是,我們可以僅使用主題來設置文本大小; 無需為每個屬性定義值。 使用主題而不是樣式為我們提供了一個巨大的優勢:我們可以以編程方式為整個視圖設置主題。

主題文件

<resources>
    <style name="FontSizeSmall">
        <item name="android:textSize">12sp</item>
    </style>
    <style name="FontSizeMedium">
        <item name="android:textSize">16sp</item>
    </style>
    <style name="FontSizeLarge">
        <item name="android:textSize">20sp</item>
    </style>
</resources>

創建一個類來處理加載我們的首選項:

public class BaseActivity extends Activity {
    @Override
    public void onStart() {
        super.onStart();

        // Enclose everything in a try block so we can just
        // use the default view if anything goes wrong.
        try {
            // Get the font size value from SharedPreferences.
            SharedPreferences settings =
                getSharedPreferences("com.example.YourAppPackage", Context.MODE_PRIVATE);

            // Get the font size option.  We use "FONT_SIZE" as the key.
            // Make sure to use this key when you set the value in SharedPreferences.
            // We specify "Medium" as the default value, if it does not exist.
            String fontSizePref = settings.getString("FONT_SIZE", "Medium");

            // Select the proper theme ID.
            // These will correspond to your theme names as defined in themes.xml.
            int themeID = R.style.FontSizeMedium;
            if (fontSizePref == "Small") {
                themeID = R.style.FontSizeSmall;
            }
            else if (fontSizePref == "Large") {
                themeID = R.style.FontSizeLarge;
            }

            // Set the theme for the activity.
            setTheme(themeID);
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }
    }

最后,通過擴展 BaseActivity 來創建活動,如下所示:

public class AppActivity extends BaseActivity{
}

由於大多數應用程序的活動數量比繼承 TextView 的 TextView 或小部件少得多。 隨着復雜性的增加,這將呈指數增長,因此該解決方案需要較少的代碼更改。

感謝雷·庫內爾

您可以使用基本活動配置放大/縮小應用程序的文本大小,使所有活動成為固有的基礎活動。

縮放正常值為 1.0,2.0 將字體大小加倍,0.50 將使其減半。

public  void adjustFontScale( Configuration configuration,float scale) {

    configuration.fontScale = scale;
    DisplayMetrics metrics = getResources().getDisplayMetrics();
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.getDefaultDisplay().getMetrics(metrics);
    metrics.scaledDensity = configuration.fontScale * metrics.density;
    getBaseContext().getResources().updateConfiguration(configuration, metrics);

}

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    adjustFontScale( getResources().getConfiguration());
}

可能的解決方案是您創建一個擴展 TextView的基類,並將此文本視圖類用作編輯文本。 希望您在第一個屏幕中要求尺寸。 在任何情況下,您都可以在基類中設置文本大小。 這將解決您的問題。

就像你在包 com.example 中創建這個類並且類名是 BaseTextView,然后在 xml 文件而不是<TextView .../>你會寫<com.example.BaseTextView ... />

希望這可以幫助。

創建一個函數並將微調器大小值傳遞為

void setSize(int size){
...
setTextSize()
// on All of the layout texts and views on screen

}

在屏幕上的所有視圖和布局文本上調用 setTextSize()。

此處查看文檔

要縮放所有組件的字體大小(意味着整個應用程序),有一種很好的方法可以通過多個設備實現和測試。 此解決方案可應用於以下情況; 靜態聲明dp大小單位(默認為 android sp),縮放到所需的字體大小等。

該解決方案類似於Usama Saeed US給出的答案,但將涵蓋所有錯誤案例。

聲明將縮放字體大小的靜態 util 方法。

    //LocaleConfigurationUtil.class
    public static Context adjustFontSize(Context context){
        Configuration configuration = context.getResources().getConfiguration();
        // This will apply to all text like -> Your given text size * fontScale
        configuration.fontScale = 1.0f;

        return context.createConfigurationContext(configuration);
    }

在您的所有活動中,覆蓋 attachBaseContext 並在 onCreate 中調用 util 方法。

   @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(LocaleConfigurationUtil.adjustFontSize(newBase));
    }

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LocaleConfigurationUtil.adjustFontSize(this);
    }

如果您使用片段,則覆蓋 onAttach 方法

@Override
public void onAttach(Context context) {
super.onAttach(LocaleConfigurationUtil.adjustFontSize(context));
}

我不確定它是否有幫助。 但是有一種叫做“SSP”的東西——文本的可伸縮大小單位。 將此添加到您的構建 gradle

implementation 'com.intuit.ssp:ssp-android:1.0.6'

這要使用

android:textSize="@dimen/_22ssp"

https://github.com/intuit/ssp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM