Android:如何以编程方式将 Activity 的主题设置为 Theme.Dialog
所以我有一个 Activity
(例如 TestActivity
),它需要充当正常的无主题 Activity
以及 Theme.Dialog< /code> 在其他地方。我试图为这两个任务重用相同的
TestActivity
。
我正在寻找动态设置主题。 代码很简单: 这是我的活动的 onCreate
,它适用于黑色背景
public void onCreate(Bundle icicle) {
if (Utility.isDialog == true)
setTheme(android.R.style.Theme_Dialog);
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
.....
,这是清单条目
<activity android:name=".TestActivity"/>
与此同时,我发现一篇文章说它不能在这里完成,这是文章 http://code.google.com/p/android/issues/detail?id=4394 。但是有一种强烈的感觉,这是可以做到的。
欢迎所有建议。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
想解决这个问题。
问题:如何使用基于对话框和全屏的相同活动。
解决方案:
@android:style/Theme.Dialog
定义您的活动.Java
文件中,检查intent
额外定义了dialog
模式。主题
设置为android.R.style.Theme
。这是默认主题
,如果您未定义任何主题,则会应用该主题。代码:
替代解决方案:
更复杂的解决方案是使用
AlertDialog
,如下所示:ArrayAdapter
扩展的ListAdapter
类。在
getCount
函数中返回1
<前><代码>@Override
公共 int getCount() { 返回 1; }
在
getView
函数中,inflate
您需要的activity
的layout
并在返回view
之前进行任何自定义。<前><代码>@Override
public View getView( int 位置, View 视图, ViewGroup 组 ) {
视图 v = 视图;
if( v == null ) {
v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <布局资源 ID>, null );
}
...在这里进行任何定制....
返回v;
}
如果您没有在
activity
class
中进行太多处理,这绝对是第二个选择,这可能是一个选项。考虑此解决方案的唯一原因可能是在
对话框
中显示它的逻辑被隔离到将其用作对话框的位置。这两种选择都对我有用,但出于显而易见的原因,我选择了第一种选择。 :-)
Would like to give a work around for this problem.
Problem : How to use the same activity as both dialog and full screen based.
Solution :
@android:style/Theme.Dialog
.Java
file, check for anintent
extra that definesdialog
mode.Theme
toandroid.R.style.Theme
. This is the defaulttheme
which is applied if you do not define any theme.Code :
Alternate Solution:
A more complex solution is to use
AlertDialog
as below:ListAdapter
class extended fromArrayAdapter
.return
1
ingetCount
functionIn the
getView
function,inflate
thelayout
of theactivity
you need and do any customization before returning theview
.This is definitely a second choice option by if you are not doing too much processing in the
activity
class
this could be an option.Only reason to consider this solution could be that the logic to show it in a
dialog
is isolated to the places where it is used as a dialog.Both the options worked for me but for obvious reasons I am taking the first option. :-)
您可以在调用
setContentView(...)
和super.oncreate()
之前使用setTheme(..)
,它应该可以正常工作you can use
setTheme(..)
before callingsetContentView(...)
andsuper.oncreate()
and it should work fine与其他几个一样,在 onCreate 中调用 setTheme (在调用 super.onCreate 之前或之后)不起作用。但是,通过重写 setTheme,我能够指定 Manifest.xml 中指定的主题之外的主题。具体来说,以下内容没有问题:
我在讨论中找到了上述内容: https://code.google.com/p/android/issues/detail?id=4394
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:
I found the above in the discussion at: https://code.google.com/p/android/issues/detail?id=4394
调用
Activity.setTheme()
< /a> 在调用setContentView()
之前的onCreate()
中。Call
Activity.setTheme()
inonCreate()
before you callsetContentView()
.在调用
super.onCreate(savedInstance)
之前使用setTheme
use
setTheme
before callingsuper.onCreate(savedInstance)
这可能不适用于您的情况,但您可以使用主题:
当屏幕较大时,它会将您的活动显示为对话框,而当屏幕较小时,它将显示为常规活动。
对话框 上的 Android 文档对此进行了介绍,并且还包含信息编写一个也可以作为全屏片段显示的对话框。
This may not be applicable in your situation, but you can use the theme:
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.
默认主题库调用:
在我的例子中,我使用的是 AppCompat 主题,因此请确保在您的 id 上引用正确的库(即):
Default theme library call:
In my case I was using AppCompat Theme, so make sure that on your id you refer the proper library (i.e.):
我知道我迟到了,但我想在这里发布一个解决方案:
检查完整的源代码 这里。这是我更改主题时使用的代码...
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...