Skip to content

Instantly share code, notes, and snippets.

@wasnot
Created May 20, 2015 11:16
Show Gist options
  • Save wasnot/ca2246a2c544e5e91085 to your computer and use it in GitHub Desktop.
Save wasnot/ca2246a2c544e5e91085 to your computer and use it in GitHub Desktop.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
public void show(){
// Viewをinflateする。
mView = LayoutInflater.from(mContext).inflate(R.layout.view, null);
// 適宜Viewをカスタマイズする
// Windowに追加するときのLayoutParamsをつくる
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.width = WindowManager.LayoutParams.MATCH_PARENT;
params.height = WindowManager.LayoutParams.MATCH_PARENT;
params.gravity = Gravity.CENTER;
// どの層にaddするか。追加できない層もたくさんある。
params.type = WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
params.format = PixelFormat.TRANSLUCENT;
// Dialogのアニメーションにするとそれっぽいです。
params.windowAnimations = android.R.style.Animation_Dialog;
// 必要に応じていろんなflagを設定します。入らなければ削ります。
params.flags = WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON
| WindowManager.LayoutParams.FLAG_DIM_BEHIND
| WindowManager.LayoutParams.FLAG_FULLSCREEN;
// 背景に影を表示してそれっぽくします。
params.dimAmount = 0.6f;
// WindowManagerを使ってViewを追加する
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(mView, params);
}
public void dismiss(){
// removeでViewの表示をけす。
mWindowManager.removeView(mView);
}
<style name="AppPanelTheme" parent="style/Theme.AppCompat">
// android:style/Theme.Panel 等を参考に。。
<item name="android:windowBackground">@color/transparent</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowFrame">@null</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowAnimationStyle">@null</item>
// こちらはTheme.Panelだとtrueですが、falseでもいいです。
// trueだとロック画面に表示できません(不要かもしれませんが)
<!--<item name="android:windowIsFloating">true</item>-->
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
// これは書かれていませんが、追加してもいいかもしれません
<item name="android:windowDisablePreview">true</item>
<item name="android:windowNoDisplay">false</item>
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment