核心代碼
Dialog dialog = new Dialog(UserInfoActivity.this,R.style.FullHeightDialog);
View view = View.inflate(UserInfoActivity.this, R.layout.dialog_layout_gender, null);
dialog.setContentView(view);
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM); // 此處可以設置dialog顯示的位置
window.setWindowAnimations(R.style.AnimBottomOut); // 添加動畫
WindowManager.LayoutParams lp = window.getAttributes();
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
window.getDecorView().setPadding(0, 0, 0, 0);
dialog.show();
佈局文件R.layout.dialog_layout_gender
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white"
android:orientation="vertical">
<TextView
android:id="@+id/tv_gender_man"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="男"/>
<TextView
android:id="@+id/tv_gender_woman"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="女"/>
<TextView
android:id="@+id/tv_gender_diss"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="取消"/>
</LinearLayout>
</LinearLayout>
動畫文件R.style.AnimBottomOut
<style name="AnimBottomOut" parent="@android:style/Animation">
<item name="android:windowEnterAnimation">@anim/push_bottom_in</item>
<item name="android:windowExitAnimation">@anim/push_bottom_out</item>
</style>
@anim/push_bottom_in
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0" />
<!-- <alpha
android:duration="100"
android:startOffset="300"
android:fromAlpha="0.8"
android:toAlpha="1.0" /> -->
</set>
@anim/push_bottom_out
<?xml version="1.0" encoding="utf-8"?>
<!-- 上下滑入式 -->
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="100%p" />
<!-- <alpha
android:duration="200"
android:startOffset="300"
android:fromAlpha="1.0"
android:toAlpha="0.5" /> -->
</set>
Dialog樣式R.style.FullHeightDialog
<style name="FullHeightDialog" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
效果圖