2013年10月23日水曜日

AppCompat は Dialog をサポートしていない

AppCompat には Dialog 系のテーマがありません。

AppCompat で用意されているテーマ
  • Theme.AppCompat
  • Theme.AppCompat.Light
  • Theme.AppCompat.Light.DarkActionBar


次のように自分でダイアログのテーマを作ることはできます。
android:windowBackground に指定する画像(下記だと @drawable/dialog_full_holo_light)を platform から取ってこないといけないですが。
  1. <resources xmlns:android="http://schemas.android.com/apk/res/android">  
  2.   
  3.     <style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>  
  4.   
  5.     <style name="AppTheme" parent="AppBaseTheme">  
  6.         <item name="android:windowBackground">@drawable/bg</item>  
  7.     </style>  
  8.   
  9.     <style name="AppTheme.Dialog">  
  10.         <item name="android:windowFrame">@null</item>  
  11.         <item name="android:windowBackground">@drawable/dialog_full_holo_light</item>  
  12.         <item name="android:windowIsFloating">true</item>  
  13.         <item name="android:windowContentOverlay">@null</item>  
  14.         <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>  
  15.         <item name="android:windowActionBar">false</item>  
  16.         <item name="android:windowNoTitle">true</item>  
  17.         <item name="android:windowActionModeOverlay">true</item>  
  18.         <item name="android:windowCloseOnTouchOutside">false</item>  
  19.     </style>  
  20. </resources>  
ただし、
  1. <item name="android:windowActionBar">true</item>  
  2. <item name="android:windowNoTitle">false</item>  
とすると、IllegalStateException が発生します。
  1. java.lang.IllegalStateException: ActionBarImpl can only be used with a compatible window decor layout  
つまり、Dialog で AppCompat の ActionBar は利用できないようになっている、ということです。


なので、View で実装した DialogFragment を用意することになるのかな。
  1. public class SimpleDialogFragment extends DialogFragment {  
  2.   
  3.     public static SimpleDialogFragment getInstance(String title, String message) {  
  4.         Bundle args = new Bundle();  
  5.         args.putString("title", title);  
  6.         args.putString("message", message);  
  7.         SimpleDialogFragment f = new SimpleDialogFragment();  
  8.         f.setArguments(args);  
  9.         return f;  
  10.     }  
  11.   
  12.     @Override  
  13.     public Dialog onCreateDialog(Bundle savedInstanceState) {  
  14.         Bundle args = getArguments();  
  15.         String title = args.getString("title");  
  16.         String message = args.getString("message");  
  17.   
  18.         View view = LayoutInflater.from(getActivity())  
  19.                                   .inflate(R.layout.fragment_dialog, null);  
  20.   
  21.         TextView tv;  
  22.         // title  
  23.         tv = (TextView) view.findViewById(R.id.title);  
  24.         tv.setText(title);  
  25.         // message  
  26.         tv = (TextView) view.findViewById(R.id.message);  
  27.         tv.setText(message);  
  28.   
  29.         Dialog dialog = new Dialog(getActivity(), R.style.AppTheme_Dialog);  
  30.         dialog.setContentView(view);  
  31.   
  32.         return dialog;  
  33.     }  
  34. }  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/title"  
  9.         style="?attr/actionBarStyle"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="?attr/actionBarSize"  
  12.         android:gravity="center_vertical"  
  13.         android:paddingLeft="16dp"  
  14.         android:paddingRight="16dp"  
  15.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  16.   
  17.     <ScrollView  
  18.         android:layout_width="match_parent"  
  19.         android:layout_height="match_parent" >  
  20.   
  21.         <TextView  
  22.             android:id="@+id/message"  
  23.             android:layout_width="match_parent"  
  24.             android:layout_height="wrap_content"  
  25.             android:layout_margin="16dip"  
  26.             android:autoLink="web|email"  
  27.             android:linksClickable="true" />  
  28.     </ScrollView>  
  29.   
  30. </LinearLayout>  




# ちなみに ActionBar Sherlock は Dialog サポートしてます

1 件のコメント:

  1. Actionbar Sherlockも4.3.0でダイアログのテーマを廃止したようです。。見当違いのことをいっていたらすみません、、
    https://github.com/JakeWharton/ActionBarSherlock/commit/601bde214b56b8fad0b4fc5aaed5af0b531b6135

    返信削除