(代碼有詳細(xì)注釋)記錄一個(gè)自定義的帶動(dòng)畫的底部彈框代碼琅绅,其余dialog都不用愁了。谱俭。奉件。
樣圖:
WeChat8b782129b1b871a2babb9047adbe053d.png
R.style.BottomDialog:
<style name="BottomDialog" parent="@style/Base.V7.Theme.AppCompat.Light.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
R.style.mypopwindow_anim_style
<style name="mypopwindow_anim_style">
<item name="android:windowEnterAnimation">@anim/popshow_anim</item>
<!-- 指定顯示的動(dòng)畫xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的動(dòng)畫xml -->
</style>
popshow_anim.xml
<?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="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
pophidden_anim
<?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="50%p" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
dialog完整代碼
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.example.admin.listviewdemo.R;
public class CustomDialog extends Dialog {
private TextView txt;
private Context context;
public CustomDialog(@NonNull Context context) {
super(context, R.style.BottomDialog);//第二個(gè)參數(shù)是不設(shè)置的話dialog將無(wú)法占滿設(shè)置的大小宵蛀,有默認(rèn)邊距
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom); //設(shè)置dialog布局
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;//設(shè)置布局屬性占滿寬度
params.height = WindowManager.LayoutParams.WRAP_CONTENT;//設(shè)置布局屬性適應(yīng)高度
params.gravity = Gravity.BOTTOM;//設(shè)置dialog位于底部
params.windowAnimations = R.style.mypopwindow_anim_style;//設(shè)置dialog進(jìn)入和出去的動(dòng)畫
window.setAttributes(params);
setCancelable(false);//設(shè)置點(diǎn)擊dialog外部無(wú)效,不關(guān)閉dialog
txt = findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hide();
}
});
}
}