問題描述
我正在將舊 Dialogs 切換為 DialogFragment,但主題和樣式似乎不起作用.
I'm switching my old Dialogs to DialogFragment, but the themes and styles don't seem to be working.
我正在使用兼容庫 v4 中的 DialogFragment,并且在 onCreate 方法中我嘗試調用 setStyle(style, theme);有很多不同的主題,但對話框總是在運行 Android 4.0.3 的模擬器中顯示為舊"對話框(即,它不顯示在 Holo 主題中).
I'm using the DialogFragment from the compatibility library v4, and in the onCreate method I've tried calling setStyle(style, theme); with a lot of different themes, but the dialog always shows as an "old" dialog in the emulator running Android 4.0.3 (i.e., it does not shows in Holo theme).
還有什么我應該做的嗎?使用兼容性庫是否會禁用 Holo 主題或任何東西?如果是這種情況,我是否應該創建兩個 DialogFragment,一個用于舊版本,一個用于新版本?
Is there anything else that I should be doing? Does using the compatibility library disables the Holo theme or anything? If this is the case, should I create two DialogFragments, one for older versions and one for newer versions?
謝謝!
這是我的對話框的(簡化的)代碼.我已經嘗試過 Theme_Holo_Dialog_NoActionBar 和 Theme_DeviceDefault_Dialog_NoActionBar,但 Android 4 模擬器總是將對話框顯示為舊"對話框,而不是使用 Holo 主題.我究竟做錯了什么?:(
Here's the (simplified) code for my dialog. I've tried both Theme_Holo_Dialog_NoActionBar and Theme_DeviceDefault_Dialog_NoActionBar, but the Android 4 emulator always shows the dialog as an "old" dialog instead of using the Holo theme. What am I doing wrong? :(
[...]
import android.support.v4.app.DialogFragment;
[...]
public class AlertDialogFragment extends DialogFragment {
public static AlertDialogFragment newInstance(int id) {
AlertDialogFragment f = new AlertDialogFragment();
Bundle args = new Bundle();
args.putInt("id", id);
f.setArguments(args);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
theme = android.R.style.Theme_Holo_Dialog_NoActionBar;
setStyle(style, theme);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
mId = getArguments().getInt("id");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(mTitle)
.setMessage(mMessage)
.setPositiveButton(getString(R.string.btn_ok), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
});
return builder.create();
}
推薦答案
我相信你需要在實際的Dialog而不是Fragment上設置主題
I believe you need to set the theme on the actual Dialog and not the Fragment
使用此構造函數創建您的 AlertDialog:
Use this constructor to create your AlertDialog:
AlertDialog.Builder(Context context, int theme)
即
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme)
這篇關于主題不適用于 Android 上的 DialogFragment的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!