Android的DialogFragment与AppCompat主题问题

我试图创build与AppCompat主题的DialoFragment,但是当我使用AppCompat主题时,不显示对话框标题。

我使用定义的风格:

<style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog"/> 

当父主题将被更改为:

 <style name="DialogFragment" parent="android:Theme.Material.Light.Dialog"/> 

要么

 <style name="DialogFragment" parent="android:Theme.Holo.Light.Dialog"/> 

标题正常显示。

我的对话框的代码:

 public class InfoDialog extends DialogFragment { public static final String TAG = InfoDialog.class.getName(); @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Dialog dialog = super.onCreateDialog(savedInstanceState); dialog.getWindow().setTitle(getString(R.string.dialog_title)); return dialog; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogFragment); } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.info_dialog, container, false); } } 

任何想法是什么造成这个问题? 应用程序使用com.android.support:appcompat-v7:22.2.0 ,也许这是平台错误?

Solutions Collecting From Web of "Android的DialogFragment与AppCompat主题问题"

尝试扩展AppCompatDialogFragment而不是DialogFragment。 这应该够了吧。

Theme.AppCompat.Light.Dialog默认不设置标题窗口。

尝试如下所示:

 <style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog"> <item name="android:windowNoTitle">false</item> </style> 

据我所知,一旦你重写onCreateView你重写dialogFragment的默认布局。 所以我build议你创build整个自定义对话框片段布局。

这里是一个示例XML布局。

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="match_parent" android:background="@drawable/rounded_dialog" android:orientation="vertical" android:padding="@dimen/margin_standard"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="@dimen/margin_standard" > <TextView android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/dialog_error_prompt_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/lorem" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_marginBottom="@dimen/margin_standard" android:layout_marginLeft="@dimen/horizontal_margin" android:layout_marginRight="@dimen/margin_standard" android:layout_marginTop="@dimen/margin_standard" > <TextView android:id="@+id/dialog_error_prompt_description" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/lorem_long" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="right" android:orientation="horizontal" android:weightSum="1" android:padding="@dimen/margin_standard"> <TextView android:visibility="invisible" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="@dimen/margin_standard" android:clickable="true" android:padding="@dimen/margin_standard" android:text="@string/cancel" android:textAppearance="?android:attr/textAppearanceButton" android:layout_weight=".70" android:gravity="center" android:textColor="@color/primary"/> <TextView android:gravity="center" android:layout_weight=".30" android:id="@+id/dialog_error_prompt_positive_button" android:layout_width="wrap_content" android:layout_height="@dimen/standard_button_height" android:layout_margin="@dimen/margin_standard" android:background="@drawable/primary_button_selector_background" android:clickable="true" android:padding="@dimen/margin_standard" android:text="@string/ok"`enter code here` android:textAppearance="?android:attr/textAppearanceButton" android:textColor="@color/background_floating_material_light"/> </LinearLayout> </LinearLayout>