简体   繁体   中英

App styling in Android Studio

I cannot seem to find the right solution to increase and decrease the text size of the whole android app project.I want to give user an option so the user can change the font size of the whole app. I tried extend my Base-activity to Text-view but didn't do the trick i hope anyone here can help me.

您必须从 .xml 文件中的 Design 选项卡更新 TextViews 的textSize ,您可以在res > layout like activity_main.xml文件中找到这些文件。

If you want to change size for whole app, Try custom themes and set it dynamically.

<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>

Create separate themes for different sizes, and use this class for setting it.

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
        }

    }

}

In your activity, call this method before super.oncreate() method,

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

Finally change theme in your code where ever you want,

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM