問題描述
所以我有一個 Activity
(比如 TestActivity
),它需要充當普通的非主題 Activity
以及 Theme.Dialog
在其他地方.我正在嘗試為這兩個任務重用相同的 TestActivity
.
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.
我正在尋找動態設置主題.代碼很簡單:這是我的活動的 onCreate
,適用于黑色背景
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);
.....
這是清單條目
<activity android:name=".TestActivity"/>
同時我發現一個帖子說它不能在這里完成是帖子 http://code.google.com/p/android/issues/detail?id=4394 .但是有一種強烈的感覺是可以做到的.
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.
歡迎所有建議.
推薦答案
想解決這個問題.
問題:如何使用相同的活動作為對話框和全屏.
Problem : How to use the same activity as both dialog and full screen based.
解決方案:
- 在您的 AndroidManifest.xml 中定義您的活動,主題為
@android:style/Theme.Dialog
- 在您各自的
.Java
文件中,檢查定義dialog
模式的intent
額外內容. - 如果不存在,請將
Theme
設置為android.R.style.Theme
.這是默認的theme
,如果您未定義任何主題,則會應用它.
- Define your activity in your AndroidManifest.xml with the theme
@android:style/Theme.Dialog
- In your respective
.Java
file, check for anintent
extra that definesdialog
mode. - If it does not exist, set the
Theme
toandroid.R.style.Theme
. This is the defaulttheme
which is applied if you do not define any theme.
代碼:
boolean fDialogMode = getIntent().hasExtra("dialog_mode");
if( ! fDialogMode ) {
super.setTheme(android.R.style.Theme);
}
替代解決方案:
一個更復雜的解決方案是使用 AlertDialog
如下:
A more complex solution is to use AlertDialog
as below:
- 定義一個從
ArrayAdapter
擴展而來的ListAdapter
類. 在
getCount
函數中返回1
- Define a
ListAdapter
class extended fromArrayAdapter
. return
1
ingetCount
function
@Override
public int getCount() { return 1; }
在getView
函數中,inflate
你需要的activity
的layout
并做任何在返回 view
之前進行自定義.
In the getView
function, inflate
the layout
of the activity
you need and do any customization before returning the view
.
@Override
public View getView( int position, View view, ViewGroup group ) {
View v = view;
if( v == null ) {
v = getSystemService(Context.LAYOUT_INFLATER_SERVICE).inflate( <layout res id>, null );
}
... Do any customization here ....
return v;
}
如果您沒有在 activity
class
中進行過多處理,這絕對是第二選擇.
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.
考慮此解決方案的唯一原因可能是在 dialog
中顯示它的邏輯與用作對話框的地方隔離.
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. :-)
這篇關于Android:如何以編程方式將 Activity 的主題設置為 Theme.Dialog的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!