问题
I've trying to create DialoFragment with AppCompat theme, but when i use AppCompat theme, dialog title is not shown.
I'am using defined style:
<style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog"/>
When parent theme will be changed to:
<style name="DialogFragment" parent="android:Theme.Material.Light.Dialog"/>
or
<style name="DialogFragment" parent="android:Theme.Holo.Light.Dialog"/>
title is displaying properly.
Code of my 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);
}
}
Any ideas what is causing the issue?
Application use com.android.support:appcompat-v7:22.2.0
, maybe this is platform bug?
回答1:
Try extending AppCompatDialogFragment instead of DialogFragment. That should do the trick.
回答2:
Theme.AppCompat.Light.Dialog
sets no title window in default.
Try something like below:
<style name="DialogFragment" parent="Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">false</item>
</style>
回答3:
As far as i know once you override onCreateView you are overriding the default layout of the dialogFragment. so i suggest that you create the whole custom dialog fragment layout.
here's a sample xml layout.
<?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>
来源:https://stackoverflow.com/questions/30854069/android-dialogfragment-with-appcompat-theme-issue