PopupWindow是一个常用的一个组件,和popwindow相似的组件,还有一个叫AlertDialog
PopupWindow的自定义相信大家一定很熟悉,这里我展示一下我的相关代码了,如有不足请指教哦
PopupWindow pop=new PopupWindow(context);
View contentView = LayoutInflater.from(context).inflate(R.layout.navigationpopup, null); //获取popupwindow样式
pop.setContentView(contentView);
pop.setWidth(LayoutParams.MATCH_PARENT);// 设置宽度
pop.setHeight(LayoutParams.WRAP_CONTENT);//设置高度
//初始化layout 上面的view
Button btn=(Button)contentView.findViewById(R.id.btn);
//添加点击事件--省略
pop.setAnimationStyle(R.style.AnimationFade_bottom);//设置动画
pop.setOutsideTouchable(true);//设置outside touch
pop.getBackground().setAlpha(0);//设置背景为透明
pop.showAtLocation(context.findViewById(R.id.ll_web), Gravity.BOTTOM, 0, 0);//设置显示
弹出动画展示给大家:
<style name="AnimationFade_bottom">
<!-- PopupWindow_bottom上下弹出的效果 -->
<item name="android:windowEnterAnimation">@anim/in_bottomtotop</item>
<item name="android:windowExitAnimation">@anim/out_toptobottom</item>
</style>
@anim/in_bottomtotop
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义从上向下进入的动画 -->
<translate
android:duration="200"
android:fromYDelta="100%"
android:toYDelta="0" />
</set>
@anim/out_toptobottom
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- 定义从下向上进入的动画 -->
<translate
android:duration="200"
android:fromYDelta="0"
android:toYDelta="100%" />
</set>
自定义的样式主要看你的layout怎么写了
这里再写一个自定义的AlertDialog,
这里我们注意到Layout的作用,当我们自定义Alert 的时候就能够对于Alert的样式进行修改,从而达到自定义的目的。
// 取得自定义View
LayoutInflater layoutInflater = LayoutInflater.from(this);
View myLoginView = layoutInflater.inflate(R.layout.login, null);
Dialog alertDialog = new AlertDialog.Builder(this).
setTitle("用户登录").
setIcon(R.drawable.ic_launcher).
setView(myLoginView).
setPositiveButton("登录", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).
create();
alertDialog.show();