AndroidでIOSと同じようなダイアログを表示する
自分が参照しづらいのでエントリ分割)
元エントリは
- AndroidでIOSと同じようなレイアウトに似せる技術の考察 - exception think
- AndroidでIOSに近づける思想の考察 - exception think
- AndroidらしいUIの考察 - exception think
- AndroidでIOSと同じようなレイアウトに似せるFWの考察 - exception think
一番よくあるのは Dialogの見た目を一致させたいという話が
な話はあるけど、こっちはうまくいかないorz
外枠が変にのこっちゃんだよね‥‥
- AndroidManifest.xml
<activity android:theme="@style/my_activity_theme"
- values/style.xml
<style name="my_activity_theme" parent="@android:style/Theme"> <item name="android:alertDialogStyle">@style/AlertDialog</item> </style> <style name="AlertDialog"> <item name="android:windowNoTitle">true</item> <item name="android:colorBackground">@null</item> <!-- <item name="android:windowBackground">@android:color/transparent</item> --> </style>
<item name="android:colorBackground">@null</item>
の指定は 背景塗りつぶさない指定*1
コード自体は下記みたいに頑張ってみたんですけどね(汗
View layout = getLayoutInflater().inflate(R.layout.dialog, null); final AlertDialog al = new AlertDialog.Builder( this ). setCancelable(false). setView(layout). create(); //show前はinfrateするレイアウト経由でアクセス TextView tx_title = (TextView) layout.findViewById(R.id.title); tx_title.setText(""); TextView tx = (TextView) layout.findViewById(R.id.body); tx.setText(R.string.default_txt); Button btn = (Button) layout.findViewById(R.id.ok); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v ) { al.dismiss(); } }); //別の場所でshow al.show(); //show後は下記のようにアクセス TextView tx_aft = (TextView)al.findViewById(R.id.body); tx_aft.setText("hogehoge");
Dialogクラスで頑張るって方針はなんとか動かしたことはある
- values/style.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <style name="Theme.CustomProgressDialog" parent="android:style/Theme.Dialog"> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> </style> </resources>
- layout/dialog.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/ios_dialog_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <!-- //〜略〜 --> </LinearLayout>
Dialog mDialog = null; if(mDialog!=null){ try{ mDialog.dismiss(); }catch(Exception ex){mDialog.cancel();} mDialog = null; } mDialog = new Dialog(this, R.style.Theme_CustomProgressDialog); mDialog.setContentView(R.layout.dialog); mDialog.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent e) { if(e.getKeyCode() != KeyEvent.KEYCODE_BACK) return false; if(e.getAction() != KeyEvent.ACTION_DOWN)return false; mDialog.cancel(); mDialog = null; return false; } }); TextView tx_title = (TextView) mDialog.findViewById(R.id.title); tx_title.setText(""); TextView tx = (TextView) mDialog.findViewById(R.id.body); tx.setText(R.string.default_txt); Button btn = (Button) mDialog.findViewById(R.id.ok); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v ) { try{ mDialog.dismiss(); }catch(Exception ex){mDialog.cancel();} mDialog = null; } }); mDialog.show();
ProgressDialogもIOSに似せて! という話でチャレンジしてみたら、以下な感じ
ProgressDialog で setContentViewすると android.util.AndroidRuntimeException: requestFeature() must be called before adding content 出てしまう。どうしよう(汗
@bina1204 そんな感じのエラーなんですけどね。。 ProgressDialog で IOSっぽいレイアウトを適応したくて‥‥‥
2013-01-15 19:22:29 via YoruFukurou to @bina1204
@bina1204 問題はrequestFutureしてるところがわからない(´▽`;)ゞ(特に指定してないので
2013-01-15 19:27:25 via hamoooooon to @bina1204
@bina1204 一応 URL で解決しましたけど、ProgressDialogのレイアウトスタイルを完全に上書きするイメージになる(クルクルスピンも消える)のでDialogで一から作るのと変わらないですね(汗
2013-01-16 11:54:46 via YoruFukurou to @bina1204
ProgressDialog mDialog = new ProgressDialog(addContext, R.style.Theme_CustomProgressDialog); mDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); mDialog.setCancelable(true); mDialog.setIndeterminate(true); mDialog.show(); //show後にレイアウトを上書き mDialog.setContentView(R.layout.dialog); mDialog.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent e) { if(e.getKeyCode() != KeyEvent.KEYCODE_BACK) return false; if(e.getAction() != KeyEvent.ACTION_DOWN)return false; mDialog.cancel(); mDialog = null; return false; } }); TextView tx_title = (TextView) mDialog.findViewById(R.id.title); tx_title.setText(""); TextView tx = (TextView) mDialog.findViewById(R.id.body); tx.setText(R.string.default_txt); Button btn = (Button) mDialog.findViewById(R.id.ok); btn.setOnClickListener(new View.OnClickListener(){ public void onClick(View v ) { try{ mDialog.dismiss(); }catch(Exception ex){mDialog.cancel();} mDialog = null; } });
でよくあるパターンで IOSっぽいダイアログ背景画像を高解像度の奴しか渡されない時
自分でサイズ調整しないとダイアログが画像に引っ張られて間延びすると。。(汗
ProgressDialogの方は
setContentView以降に設定してねの感じ・・
Resources m_r = getResource(); DisplayMetrics metrics = m_r.getDisplayMetrics(); int dialogWidth = (int) (metrics.widthPixels * 0.9); int dlg_height_p = 80;//m_r.getInteger(R.integer.dlg_height_p); if("KDDI".equals(Build.BRAND) && "IS03".equals(Build.MODEL))dlg_height_p += 5; int dialogHeight = (int) (metrics.heightPixels * dlg_height_p * 1/100); WindowManager.LayoutParams lp = mDialog.getWindow().getAttributes(); lp.width = dialogWidth; lp.height =dialogHeight; mDialog.getWindow().setAttributes(lp);
ココらへんのお話と同じ対応なわけだけど
意外と知られていなかった技ImageView iv = (ImageView) findViewById(R.id.hoge);iv.getLayoutParams().width = width;iv.getLayoutParams().height = height;
2013-02-19 07:51:14 via web
AndroidでViewの大きさをXMLのLayoutのままで、javaコード上で調整できて、しかも短いコードで実装できる方法。たしかにこれ、ググってもなくて自分で見つけたやつだ。最初、いろいろと悪戦苦闘したことの応用だった気がする。まぁたまに不自由なところがあるのだけども。
2013-02-19 07:57:31 via web
通常は
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/dialog_ios" android:orientation="vertical" android:layout_width="wrap_content" //☆ android:layout_height="wrap_content"> //☆
の ☆の 指定でうまく自動計算するはずなので
余程のものでなければ上記のレイアウト任せで諦める
という手はありますが。。*2
だからといってdimens.xml地獄も厳しいわけですし。
一番理想はアニメパターンみたいにlayout.xmlのwidth等に
みたいな指定ができるといいんですけどね
10%p
10%
ちなみにWindowsみたいにダイアログをモードレスダイアログにしたいという要望があったりする
その場合は
Window window = mDialog.getWindow(); window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL, WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL); window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND); mDialog.show();
な対応が必要。
あたりを参照してる*3
ただ本来Androidじゃモーダルではないらしい
備考)
AlertDialog系のDialog設定は
- AlertDialog.BuilderとLayoutInflaterによりダイアログにレイアウトを流し込む - Kazzzの日記
- Androidアプリ開発!!: アラートダイアログにレイアウトを読み込ませる(AlertDialog, inflate)
- ダイアログの内容を自由に変更する方法 | GE Android Blog
IOSのAlertみたいに表示中は背景を半透明黒にしたい場合)
これはActivityの場合の対処メモ
ようは某社さんの全画面広告で、背景を黒半透明にしたいみたいな要望があったわけで・・(汗
企画の方は IOSベースで考えるから IOSと似たような表示にならないと違和感感じるんだろうな。。。というお話
で、実際の対処
の2番めを使う
- res/style.xml
<style name="Theme.DimPanel" parent = "@android:style/Theme.Panel"> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:backgroundDimAmount">0.5</item> </style>
- AndroidManifest.xml
<activity android:name="jp.basicinc.gamefeat.android.sdk.view.GameFeatPopupActivity" android:configChanges="orientation|keyboardHidden" android:theme="@style/Theme.DimPanel" android:label="GameFeat" /> <!-- android:theme="@android:style/Theme.Translucent.NoTitleBar" -->
類似>