PopupWindow、Activity做伪弹窗

来源:互联网 发布:淘宝客服外包培训视频 编辑:程序博客网 时间:2024/04/29 07:38
1. PopupWindow
 直接上代码:
public OnClickListener menu_press=new OnClickListener() {@Overridepublic void onClick(View v) {//获取LayoutInflater实例 LayoutInflaterinflater = (LayoutInflater)PopActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); popuView=inflater.inflate(R.layout.alert_dialog, null);//怎样将我的View加入到PopupWindow中呢???menuWindow2=new PopupWindow(popuView,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); //设置如下四条信息,当点击其他区域使其隐藏,要在show之前配置 menuWindow2.setFocusable(true); menuWindow2.setOutsideTouchable(true); menuWindow2.update(); menuWindow2.setBackgroundDrawable(new BitmapDrawable()); menuWindow2.showAtLocation(PopActivity.this.findViewById(R.id.popLayout), Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0,50); //设置layout在PopupWindow中显示的位置 Button btn_take_photo = (Button)popuView.findViewById(R.id.btn_take_photo);//这里这个popuView不能少,指示是哪个View上的控件。Button btn_pick_photo = (Button)popuView.findViewById(R.id.btn_pick_photo);Button btn_cancel = (Button) popuView.findViewById(R.id.btn_cancel);//下面对每一个Layout进行单击事件的注册吧。。。 btn_take_photo.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {menuWindow2.dismiss();}});}};
alert_dialog.xml中只定义了3个Button.还可以设置它的弹出方式:// 设置SelectPicPopupWindow弹出窗体动画效果menuWindow2.setAnimationStyle(R.style.AnimBottom);这个动画方式在 style.xml 中定义:
<style name="AnimBottom" parent="@android:style/Animation">        <item name="android:windowEnterAnimation">@anim/push_bottom_in</item>        <item name="android:windowExitAnimation">@anim/push_bottom_out</item>    </style>
 可以参考这篇文章:

手把手教会popupWindow从下往上弹出效果的实现




2. Activity做伪弹窗
Activity:
public class ExitActivity extends Activity {private LinearLayout layout;@Overrideprotected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);setContentView(R.layout.exit_dialog);layout = (LinearLayout) findViewById(R.id.exit_layout);layout.setOnClickListener(new OnClickListener() {public void onClick(View v) {Toast.makeText(getApplicationContext(), "提示:点击窗口外部关闭窗口!",Toast.LENGTH_SHORT).show();}});Button button0 = (Button) layout.findViewById(R.id.exitBtn0);Button button1 = (Button) layout.findViewById(R.id.exitBtn1);button0.setOnClickListener(new OnClickListener() {public void onClick(View v) {finish();}});button1.setOnClickListener(new OnClickListener() {public void onClick(View v) {finish();}});}/*** * 关键是如何做到点击其他地方退出和如何关闭程序。 * 1、点击其他地方退出,只要监听OnClickListener让所有的点击都退出就行了(除了xml中指定按钮事件之外)。 * 2、退出主程序。只要在退出程序中将主程序自己设置成静态对象,在外部调用即可 public static MainWeixin * instance=null; * **/public boolean onTouchEvent(MotionEvent event) {finish();return true;}}
exit_dialog.xml:
<?xml version="1.0" encoding="UTF-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/exit_layout"    android:layout_width="280dp"    android:layout_height="wrap_content"    android:background="@color/cadetblue"    android:gravity="center_horizontal"    android:orientation="vertical" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="4dp"        android:padding="5dp"        android:text="退出微信"        android:textColor="#333"        android:textSize="20sp" />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="1dp"        android:gravity="center_horizontal"        android:padding="10dp"        android:text="退出后,你将收不到新的消\n息.确定要退出?"        android:textColor="#333"        android:textSize="16sp" />    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginBottom="8dp"        android:layout_marginTop="33dp" >        <Button            android:id="@+id/exitBtn0"            android:layout_width="110dp"            android:layout_height="wrap_content"            android:background="@drawable/btn_button_pressed"                        android:gravity="center"            android:onClick="exitbutton0"            android:text="是"            android:textColor="#fff"            android:textSize="16sp" />        <Button            android:id="@+id/exitBtn1"            android:layout_width="110dp"            android:layout_height="wrap_content"            android:layout_marginLeft="10dp"            android:background="@color/white"            android:gravity="center"            android:onClick="exitbutton1"            android:text="否"            android:textColor="#333"            android:textSize="16sp" />    </LinearLayout></LinearLayout>
style.xml中的定义:
 <style name="MyDialogStyle">        <item name="android:windowBackground">@android:color/transparent</item>        <item name="android:windowFrame">@null</item>        <item name="android:windowNoTitle">true</item>        <item name="android:windowIsFloating">true</item>        <item name="android:windowIsTranslucent">true</item>        <item name="android:windowContentOverlay">@null</item>        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>        <item name="android:backgroundDimEnabled">true</item>    </style><!-- 重点1、<item name="android:windowBackground">@android:color/transparent</item>  窗口背景色 重点2、<item name="android:windowFrame">@null</item>  Dialog的windowFrame框为无 重点4、<item name="android:windowIsFloating">true</item>  是否浮现在activity之上 重点5、<item name="android:windowIsTranslucent">true</item>  窗口是否半透明——是(与第一条配合使用) 重点6、<item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>  窗口弹出效果 重点7、<item name="android:backgroundDimEnabled">true</item>    是否允许背景模糊 重点8、<item name="android:windowContentOverlay">@null</item>  这个不设置的话,可能会出现边框黑线  -->
在Manifest.xml中使用这个style:
<activity android:name="com.gridview.pupwindow.ExitActivity"
              android:theme="@style/MyDialogStyle"      ></activity>


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 卫生间埋在地下的水管漏水怎么办 埋在地下的水管冻住了怎么办 洗脸盆下水管有眉笔掉堵了怎么办 楼上打空调眼导致墙面潮湿怎么办 阳台做防水了又贴砖还漏水怎么办 洗菜盆里的盖子吸住拔不起来怎么办 掉小区阴井盖里物业不管怎么办 卫生间下水道被头发堵了怎么办妙招 久量时尚夜光台灯不亮了怎么办 我想做水电暖工程没有资质怎么办 修手机被弄丢了没有开维修单怎么办 手机去维修被老板弄丢了怎么办 5年保修期过后房子楼顶漏水怎么办 墙内水管漏水怎么办3步骤巧维修 下面有地暖下面水管漏水怎么办 热水器接的塑料水管子漏水怎么办 电热水器烧热水时热水管漏水怎么办 户户通位置锁定模块异常1怎么办 户户通位置锁定模块异常3怎么办 逆战安全系统检测客户端异常怎么办 电视户户通位置信息模块异常怎么办 苹果手机wi-fi模块坏了怎么办 xp本地连接受限制或无连接怎么办 公司没帮二级建造师延续注册怎么办 考造价师考过了年限不够怎么办? 上海众非称重不显示数字怎么办 宝骏630防火墙隔热棉掉了怎么办 冷车启动水温报警灯亮了怎么办 租铺位面积比合同面积小怎么办 农村村民建房领居不肯签名怎么办 车祸死者家属不来协商赔偿怎么办 26岁想回去当老师了怎么办 面包车排量小空调带不凉快怎么办 科三包过不给退钱人跑了怎么办 护师职称考试成绩单丢了怎么办 河南二级建造师报名地址填错怎么办 山东以前的企业没有消防备案怎么办 重庆渝北初级审核时间过了怎么办 初级职称复核毕业证弄丢了怎么办 杭州公租房选房后变更单位的怎么办 契税交了贷款办不下来怎么办