自定义AlertDialog ContentView、Position
效果图:
代码:
public void showCustomDialog(){
LinearLayout customView = new CustomDialogView(this);
AlertDialog mDialog = new AlertDialog.Builder(this,R.style.DialogAtBottom)
.setView(customView)
.setTitle(null)
.create();
Window window = mDialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
window.setGravity(Gravity.BOTTOM);
window.setAttributes(lp);
mDialog.show();
}
<style name="Animation.Dialog.ShowAtBottom" parent="android:Animation">
<item name="android:windowEnterAnimation">@anim/slide_in_from_bottom</item>
<item name="android:windowExitAnimation">@anim/slide_out_from_bottom</item>
</style>
<style name="DialogAtBottom" >
<item name="android:windowBackground">@drawable/dialog_show_at_bottom</item>
<item name="android:windowMinWidthMajor">100%</item>
<item name="android:windowMinWidthMinor">100%</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowAnimationStyle">@style/Animation.Dialog.ShowAtBottom</item>
</style>
dialog_show_at_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android" >
<shape android:shape="rectangle">
<corners android:radius="1dp" />
<solid android:color="#ffffff" />
</shape>
</inset>
slide_in_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:fromYDelta="50%p" android:toYDelta="0"
android:duration="150"/>
slide_out_from_bottom.xml
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromYDelta="0" android:toYDelta="50%p"
android:duration="150"/>
AlertDialog实现旋转屏幕不消失
在AndroidManifest activity中加入下面设置
android:configChanges="keyboardHidden|orientation|screenSize"
据说2.3及以下的版本只需要
orientation|keyboardHidden
即可。