Android:How to programmatically set an Activity

2019-01-22 17:31发布

So I have an Activity (say TestActivity) which needs to act as a normal unthemed Activity as well as a Theme.Dialog at other place. I am trying to reuse same TestActivity for both the tasks.

All I am looking for setting the theme dynamically. The code is simple: Here is my activity's onCreate that works with a black background

public void onCreate(Bundle icicle) {
    if (Utility.isDialog == true)
        setTheme(android.R.style.Theme_Dialog);
    super.onCreate(icicle);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
.....

and here is the Manifest Entry

<activity android:name=".TestActivity"/>

And in the meantime I found a post that says it can't be done here is the post http://code.google.com/p/android/issues/detail?id=4394 .But there is a strong feeling that it can be done.

All suggestions are welcome.

9条回答
别忘想泡老子
2楼-- · 2019-01-22 17:57

Like several others, calls to setTheme in onCreate (before or after my call to super.onCreate) did not work. However, by overriding setTheme, I was able to specify a theme other than that stated in Manifest.xml. Specifically, the following worked without issue:

@Override
public void setTheme(int resid) {
    boolean changeTheme = true;
    super.setTheme(changeTheme ? android.R.style.Theme_Dialog : resid);
}

I found the above in the discussion at: https://code.google.com/p/android/issues/detail?id=4394

查看更多
贼婆χ
3楼-- · 2019-01-22 17:59

I know that I am late but I would like to post a solution here:

Check the full source code here. This is the code I used when changing theme...

SharedPreferences pref = PreferenceManager
       .getDefaultSharedPreferences(this);
String themeName = pref.getString("prefSyncFrequency3", "Theme1");
if (themeName.equals("Africa")) {
    setTheme(R.style.AppTheme);
} else if (themeName.equals("Colorful Beach")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.beach);
} else if (themeName.equals("Abstract")) {
    //Toast.makeText(this, "set theme", Toast.LENGTH_SHORT).show();
    setTheme(R.style.abstract2);
} else if (themeName.equals("Default")) {
    setTheme(R.style.defaulttheme);
}
查看更多
做自己的国王
4楼-- · 2019-01-22 18:00

use setTheme before calling super.onCreate(savedInstance)

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-22 18:07

This may not be applicable in your situation, but you can use the theme:

Theme.Holo.DialogWhenLarge

and it will display your activity as a dialog when the screen is large, and as a regular activity when the screen is small. This is covered in the Android documentation on Dialogs and also contains information on programming a Dialog which can also sun as a full screen fragment.

查看更多
我命由我不由天
6楼-- · 2019-01-22 18:09

Default theme library call:

super.setTheme(android.R.style.Theme);

In my case I was using AppCompat Theme, so make sure that on your id you refer the proper library (i.e.):

super.setTheme(android.support.v7.appcompat.R.style.Theme_AppCompat_NoActionBar);

查看更多
聊天终结者
7楼-- · 2019-01-22 18:17

Call Activity.setTheme() in onCreate() before you call setContentView().

查看更多
登录 后发表回答