【问题标题】:How to change application theme by choosing option from a listview or spinner in android?如何通过从 android 的列表视图或微调器中选择选项来更改应用程序主题?
【发布时间】:2017-09-28 12:56:17
【问题描述】:

在每次点击项目时,应将提及的项目应用于应用程序。

但是当我在手机中运行应用程序并选择一个选项时,主题不会改变,吐司也不会出现。

没有错误出现,应用程序也没有崩溃。它只是不起作用。

而且我愿意通过一个微调器来做到这一点。我该怎么办?

这是我的代码:

final ListView themeList = (ListView) findViewById(R.id.themeList);

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    if (position == 0) {
        getApplicaiton().setTheme(R.style.themeOne);
    } else if (position == 1) {
        getApplicaiton().setTheme(R.style.themeTwo);
    } else if (position == 2) {
        getApplicaiton().setTheme(R.style.themeThree);
    } else {
        Toast.makeText(ThemeActivity.this, "Something went wrong, please try again!", Toast.LENGTH_SHORT).show();
    }
};

【问题讨论】:

标签: java android themes android-theme android-styles


【解决方案1】:

根据 Android 网站的 setTheme 文档

为此上下文设置基本主题。请注意,这应该在上下文中实例化任何视图之前调用(例如,在调用 setContentView(View) 或 inflate(int, ViewGroup) 之前)。

所以你需要在此之前进行更改。

【讨论】:

    【解决方案2】:

    setTheme - 在创建任何视图之前必须调用该方法。 所以要真正“改变”你的主题,你必须重新创建你的活动并在你打电话给setContentView之前打电话给setTheme(或者甚至在你打电话给super.onCreate之前。

    另一种解决方案是不更改主题,而是动态更改元素本身(如 acitonbar、notificationbar 等)。几乎所有可以设置为主题的属性也可以在您的 java 代码中设置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 2020-04-22
      • 2017-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      相关资源
      最近更新 更多