2015-06-15 51 views
8

我試圖創建與AppCompat主題的DialoFragment,但是當我使用AppCompat主題時,不顯示對話框標題。與AppCompat主題問題的Android DialogFragment

我'使用定義的樣式:

<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,也許這是平臺錯誤?

+0

您是否找到正確的解決方案? – Ewoks

回答

0

據我所知,一旦你重寫onCreateView,你正在覆蓋dialogFragment的默認佈局。所以我建議你創建整個自定義對話框片段佈局。

這裏是一個示例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> 
+0

這是僞造的標題對話框..一些元素缺失..像標題和對話框其餘部分之間的線分隔。 – Ewoks

2

Theme.AppCompat.Light.Dialog默認情況下不設置標題窗口。

嘗試類似如下:

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