popupwindow类似微信

本文介绍了一种在Android应用中实现PopupWindow自定义显示的方法,包括如何设置出入动画、触摸事件处理及背景颜色配置等关键技术点。通过具体实例代码展示了如何加载布局资源文件、初始化PopupWindow对象并设置其监听器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/**
* 更多标签显示
*/
private void showPopupWindow() {
View contentView = getLayoutInflater().inflate(R.layout.popup_layout_fwsd_flags, null);
popupWindow = new PopupWindow(contentView, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT, true);
popupWindow.setOutsideTouchable(true);
popupWindow.setAnimationStyle(R.style.add_new_style);// 出入动画
popupWindow.setTouchInterceptor(new OnTouchListener() {


@Override
public boolean onTouch(View v, MotionEvent event) {
Log.i("mengdd", "onTouch : ");
// 这里如果返回true的话,touch事件将被拦截
// 拦截后 PopupWindow的onTouchEvent不被调用,这样点击外部区域无法dismiss
return false;
}
});
ColorDrawable cd = new ColorDrawable(-0000);  
        popupWindow.setBackgroundDrawable(cd); 
        popupWindow.showAtLocation(findViewById(R.id.ll_sxd_school), Gravity.BOTTOM, 0, 0);
        TextView tvDismiss = (TextView)contentView.findViewById(R.id.tv_popup_fwsd_flags_dismiss);
        ListView lvPopup = (ListView)contentView.findViewById(R.id.lv_popup_fwsd_flags);
        popupFlagsAdapter = new PopupFlagsAdapter(context, listFlag);
        lvPopup.setAdapter(popupFlagsAdapter);
        lvPopup.setOnItemClickListener(new OnItemClickListener() {


@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long id) {
popupWindow.dismiss();
Intent intent = new Intent(SXDSchoolActivity.this,SXDSchoolHDActivity.class);
intent.putExtra("bean",popupFlagsAdapter.getItem(position));
intent.putExtra("pg_Id",pioneers_Group.getPg_Id()+"");//groupid
startActivity(intent);
}
});
        tvDismiss.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
popupWindow.dismiss();
}
});

}


//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~anim

//----add_new_in.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>

//----------------add_new_out.xml

<?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>


//~~~~~~~~~~~~~~~~~~~~values

 <style name="add_new_style">
        <item name="android:windowEnterAnimation">@anim/add_new_in</item>
        <item name="android:windowExitAnimation">@anim/add_new_out</item>
    </style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值