gpt4 book ai didi

android - 从服务器获取主题颜色并在整个android应用程序中更改它的好方法是什么?

转载 作者:行者123 更新时间:2023-12-05 00:08:36 26 4
gpt4 key购买 nike

我正在开发一个 android 应用程序,我将从后端服务器获取颜色和字体,因为管理面板中有一个选项可以更改应用程序的颜色。这意味着管理员可以从他的面板更改颜色,我必须在 android 应用程序中反射(reflect)它。

现在,我需要显示资源文件中的默认颜色,并且如果管理员从他的面板中添加或更改颜色,我还需要更改这些颜色值。

我在研究过程中得到的东西:

  • 我已经设置了创建主题的颜色,但我们无法在运行时更改主题值。
  • 将颜色存储在共享首选项中并在任何地方手动设置它们。

  • 做到这一点的最佳方法是什么?

    最佳答案

    public class ChangeThemeActivity extends Activity implements OnClickListener
    {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
    super.onCreate(savedInstanceState);
    Utils.onActivityCreateSetTheme(this);
    setContentView(R.layout.main);

    findViewById(R.id.button1).setOnClickListener(this);
    findViewById(R.id.button2).setOnClickListener(this);
    findViewById(R.id.button3).setOnClickListener(this);
    }
    @Override
    public void onClick(View v)
    {
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case R.id.button1:
    Utils.changeToTheme(this, Utils.THEME_DEFAULT);
    break;
    case R.id.button2:
    Utils.changeToTheme(this, Utils.THEME_WHITE);
    break;
    case R.id.button3:
    Utils.changeToTheme(this, Utils.THEME_BLUE);
    break;
    }
    }
    }

    实用程序.java
    public class Utils
    {
    private static int sTheme;
    public final static int THEME_DEFAULT = 0;
    public final static int THEME_WHITE = 1;
    public final static int THEME_BLUE = 2;
    /**
    * Set the theme of the Activity, and restart it by creating a new Activity of
    the
    same type.
    */
    public static void changeToTheme(Activity activity, int theme)
    {
    sTheme = theme;
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()));
    }
    /** Set the theme of the activity, according to the configuration. */
    public static void onActivityCreateSetTheme(Activity activity)
    {
    Context ctx = getContext();
    switch (sTheme)
    {
    default:
    case THEME_DEFAULT:
    ctx.getResources().getIdentifier("BLUE_" + colorName, "color",
    ctx.getPackageName());

    break;

    }

    其中 colorname 是来自服务器的您的动态颜色

    关于android - 从服务器获取主题颜色并在整个android应用程序中更改它的好方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54326714/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com