簡體   English   中英

Android Studio 中的應用樣式

[英]App styling in Android Studio

我似乎找不到合適的解決方案來增加和減少整個 android 應用程序項目的文本大小。我想給用戶一個選項,以便用戶可以更改整個應用程序的字體大小。 我嘗試將我的 Base-activity 擴展到 Text-view,但沒有成功,我希望這里的任何人都可以幫助我。

您必須從 .xml 文件中的 Design 選項卡更新 TextViews 的textSize ,您可以在res > layout like activity_main.xml文件中找到這些文件。

如果您想更改整個應用程序的大小,請嘗試自定義主題並動態設置。

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="android:textSize">16sp</item>
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>


<style name="AppTheme2" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:textSize">15sp</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
 <style name="AppTheme3" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="android:textSize">17sp</item>
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>

為不同的大小創建單獨的主題,並使用此類進行設置。

public class themeUtils
{
    public static int cTheme;
    public static void changeToTheme(Activity activity, int theme)
    {
        cTheme = theme;
        activity.finish();
        activity.startActivity(new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));

    }

    public static void onActivityCreateSetTheme(Activity activity)
    {
        try
        {
            activity.setTheme(cTheme);
        }
        catch (Exception e)
        {
            activity.setTheme(R.style.AppTheme); //default
        }

    }

}

在您的活動中,在 super.oncreate() 方法之前調用此方法,

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        themeUtils.onActivityCreateSetTheme(this);
        super.onCreate(savedInstanceState);
        ...}

最后在您想要的任何地方更改代碼中的主題,

  themeUtils.changeToTheme(this, themeselected);//your theme name

暫無
暫無

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

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