Java 如何在android中通过从列表视图或微调器中选择选项来更改应用程序主题?

Java 如何在android中通过从列表视图或微调器中选择选项来更改应用程序主题?,java,android,themes,android-theme,android-styles,Java,Android,Themes,Android Theme,Android Styles,在每次单击项目时,应将提及的项目应用于应用程序 但当我在手机上运行应用程序并选择一个选项时,主题不会改变,祝酒词也不会出现 没有显示错误,应用程序也没有崩溃。它只是不起作用 我愿意用旋转器来做这件事。我该怎么办 这是我的密码: final ListView themeList = (ListView) findViewById(R.id.themeList); @Override public void onItemClick(AdapterView<?> parent, View

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

但当我在手机上运行应用程序并选择一个选项时,主题不会改变,祝酒词也不会出现

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

我愿意用旋转器来做这件事。我该怎么办

这是我的密码:

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();
    }
};
final ListView themeList=(ListView)findViewById(R.id.themeList);
@凌驾
public void onItemClick(AdapterView父对象、视图、整型位置、长id){
如果(位置==0){
getapplicationon().setTheme(R.style.themeOne);
}否则如果(位置==1){
getapplicationon().setTheme(R.style.themeTwo);
}否则如果(位置==2){
getapplicationon().setTheme(R.style.themeThree);
}否则{
Toast.makeText(ThemeActivity.this,“出现问题,请重试!”,Toast.LENGTH\u SHORT.show();
}
};

根据Android网站的setTheme文档

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


因此,您需要在此之前进行更改。

在创建任何视图之前,必须调用
setTheme
-方法。 因此,要真正“更改”主题,您必须在调用
setContentView
(或者甚至在sou-call
super.onCreate
之前)之前重新创建活动并调用
setTheme


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

按照本教程操作如果更改主题,则必须重新启动活动才能查看更改。