2012-05-14 135 views
39

我將舊對話框切換爲DialogFragment,但主題和樣式似乎不起作用。我使用兼容性庫v4中的DialogFragment,並且在我嘗試調用setStyle(style,theme)的onCreate方法中使用DialogFragment;有很多不同的主題,但對話框始終在運行Android 4.0.3的模擬器中顯示爲「舊」對話框(即,它不會顯示在Holo主題中)。主題不適用於Android上的DialogFragment

還有什麼我應該做的?使用兼容性庫是否會禁用Holo主題或任何其他內容?如果是這種情況,我應該創建兩個DialogFragments,一個用於舊版本,一個用於較新版本?

謝謝!


下面是我的對話框的(簡化)代碼。我已經嘗試了Theme_Holo_Dialog_NoActionBar和Theme_DeviceDefault_Dialog_NoActionBar,但Android 4仿真器始終將對話框顯示爲「舊」對話框,而不是使用Holo主題。我究竟做錯了什麼? :(

[...] 
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(); 
    } 
+0

,您在繼承什麼風格,展現我們也styles.xml你有什麼targetSdk?它需要超過11 – Blundell

+0

我的目標是SDK 14,我甚至沒有使用styles.xml文件,我只是調用setStyle(style,theme);在DialogFragment的onCreate方法中嘗試風格和主題的多種組合。 例如,在我的「舊」對話框中,我使用了android.R.style.Theme_DeviceDefault_Dialog_NoActionBar,它在ICS仿真器中顯示爲Holo,但現在使用的是不同的主題。 – LuTHieR

+0

嘗試隱式使用Holo:'android.R.style.Theme_Holo_Dialog' – Blundell

回答

25

我相信你需要設置實際對話主題,而不是片段

使用此構造函數來創建你的AlertDialog:

AlertDialog.Builder(Context context, int theme) 

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), theme) 
+0

我試着它,它似乎工作,謝謝:)我只需要做一些更多的測試,然後我會發布結果,並標記爲答案接受。謝謝! – LuTHieR

+0

@LuTHieR這是否解決了您的問題? –

+0

不完全...最後,我爲每個Android版本創建了不同的XML佈局文件,而且這似乎是做了詭計... – LuTHieR

47

你不應該使用支持庫的AlertDialog.Builder(Context,int)構造函數,因爲它是唯一可用的API以來11

要設置一個主題,你的對話,而不是使用像這樣的ContextThemeWrapper:

ContextThemeWrapper context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Dialog_NoActionBar); 
AlertDialog.Builder builder = new AlertDialog.Builder(context); 
+5

有沒有人試過用自定義樣式?當我傳入一個預定義的android風格時,它可以工作,但是如果我傳遞自己的風格,則不適用。(我的自定義風格是Theme.Dialog的子元素) – ashutosh

+0

如果您完全不使用構建器,爲我工作的自定義樣式)給出:http://stackoverflow.com/questions/13469084/dialogfragment-in-android-with-theme – Almer

-4

你應該在「onCreateView」寫這些代碼。

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
    View view = inflater.inflate(R.layout.dialog_your_theme, container); 
    return view; 
} 
+0

雖然此代碼塊可能會回答這個問題,這將是最好的,如果你可以提供爲什麼它這麼做很少解釋。 – David

17

我只是失去了很多時間,但我終於找到了一種完全在xml中完成此任務的方法。

在應用程序主題中,對話框實際上是單獨設置的主題。因此,爲了風格的綠色按鈕和綠色的EditText暗示所有DialogFragments,你會作出這樣的風格:

<style name="DialogTheme" parent="@android:style/Theme.Holo.Light.Dialog"> 
    <item name="android:buttonStyle">@style/button_green</item> 
    <item name="android:textColorHint">@color/green</item> 
</style> 

這個主題然後添加到應用程序的主題爲dialogTheme

<style name="MyTheme" parent="android:Theme.Holo.Light"> 
    <item name="android:dialogTheme">@style/DialogTheme</item> 
</style> 

非常感謝誰寫this post爲我展示了我一直在尋找的路徑!

2

下面是一個更爲當前的答案,使用目標SDK的23和最小SDK的14,這段代碼適合我。

問題中代碼的主要變化是在構造函數中設置主題,僅覆蓋onCreateDialog(),並使用v7支持庫中的AlertDialog類。

使用此代碼,綠色文本平面(無邊框)按鈕顯示在4.4.4上,而不是帶有邊框的默認灰色按鈕。

import android.support.v7.app.AlertDialog; 
import android.app.Dialog; 
import android.support.v4.app.DialogFragment; 
import android.content.DialogInterface; 
import android.os.Bundle; 

public class MyDialog extends DialogFragment { 

    String message; 

    public MyDialog(String m) { 
     message = m; 
     int style = DialogFragment.STYLE_NORMAL, theme = 0; 
     theme = android.R.style.Theme_Holo_Dialog_NoActionBar; 
     setStyle(style, theme); 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 

     return new AlertDialog.Builder(getActivity()) 
       .setMessage(message) 
       .setPositiveButton("OK", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((EnterPhoneNumberActivity)getActivity()).doPositiveClick(); 
         } 
        } 
       ) 
       .setNegativeButton("EDIT", 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          ((EnterPhoneNumberActivity)getActivity()).doNegativeClick(); 
         } 
        } 
       ) 
       .create(); 
    } 
} 

用法在AppCompatActivity:

String message = "test"; 
    if (message != null) { 
     DialogFragment newFragment = new MyDialog(message); 
     newFragment.show(getSupportFragmentManager(), "dialog"); 
    }