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