(代码有详细注释)记录一个自定义的带动画的底部弹框代码,其余dialog都不用愁了。。。
样图:
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>
<!-- 指定显示的动画xml -->
<item name="android:windowExitAnimation">@anim/pophidden_anim</item>
<!-- 指定消失的动画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);//第二个参数是不设置的话dialog将无法占满设置的大小,有默认边距
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState?) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_custom); //设置dialog布局
Window window = getWindow();
WindowManager.LayoutParams params = window.getAttributes();
params.width = WindowManager.LayoutParams.MATCH_PARENT;//设置布局属性占满宽度
params.height = WindowManager.LayoutParams.WRAP_CONTENT;//设置布局属性适应高度
params.gravity = Gravity.BOTTOM;//设置dialog位于底部
params.windowAnimations = R.style.mypopwindow_anim_style;//设置dialog进入和出去的动画
window.setAttributes(params);
setCancelable(false);//设置点击dialog外部无效,不关闭dialog
txt = findViewById(R.id.txt);
txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
hide();
}
});
}
}