好记性不如烂笔头,以前我也不写博客的,现在发现以前写的东西好多都忘了,原来的思路都不记的了,所以现在要养成学到新的知识就要写博客记下来,顺便还可以帮助其他人.
先上效果图
这是一个从底部弹出Popupwindow,
private void showPopwindow() {
// 利用layoutInflater获得View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.popupwindow, null);
// 下面是两种方法得到宽度和高度 getWindow().getDecorView().getWidth()
PopupWindow window = new PopupWindow(view,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT);
// 设置popWindow弹出窗体可点击,这句话必须添加,并且是true
window.setFocusable(true);
// 实例化一个ColorDrawable颜色为半透明
ColorDrawable dw = new ColorDrawable(0xb0000000);
window.setBackgroundDrawable(dw);
// 设置popWindow的显示和消失动画
window.setAnimationStyle(R.style.mypopwindow_anim_style);
// 在底部显示
window.showAtLocation(MainActivity.this.findViewById(R.id.button),
Gravity.BOTTOM, 0, 0);
// 这里检验popWindow里的button是否可以点击
Button first = (Button) view.findViewById(R.id.first);
first.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("第一个按钮被点击了");
}
});
//popWindow消失监听方法
window.setOnDismissListener(new OnDismissListener() {
@Override
public void onDismiss() {
System.out.println("popWindow消失");
}
});
}
当有window.setFocusable(false);的时候,说明PopuWindow不能获得焦点,即使设置设置了背景不为空也不能点击外面消失,只能由dismiss()消失.
window.setFocusable(true);的时候,点击外面和Back键才会消失。
所用到的布局代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:background="#00000000"
android:orientation="vertical">
<Button
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="18dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/shape2"
android:text="更换个人头像" />
<Button
android:id="@+id/two"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="18dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/shape2"
android:text="注释" />
<Button
android:id="@+id/third"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="18dp"
android:layout_marginBottom="3dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/shape2"
android:text="修改密码" />
<Button
android:id="@+id/four"
android:layout_width="match_parent"
android:layout_height="40dp"
android:textSize="18dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="14dp"
android:background="@drawable/shape2"
android:text="取消" />
</LinearLayout>
Popupwindow 里面添加listview 需要注意;、
1.如果使用系统的适配器给listview 添加数据的时候 listview item能点击,但是如何使用自定义的适配器的话就不能点击listview,原因是因为自定义适配器里面的布局里面用到了Button,button会自己截获点击事件(在listview 当中使用Button 是一个常见的坑),所以要把 Button 改成 Textview 但是textview没有button 好看,所以把Textview 的style 改成@android/Widget.Button 但是TextView还是有一点太丑了。然后我们继续作,将TextView的style改成的@android:style/Widget.Button。这时我们内心一定很高兴,毕竟还解决了UI方面的问题,但是我们一运行,又捕获不到ListView当中的点击事件了。
一定是刚才的Button的style的问题,但是我们只是改了一个Style啊。
查了一些文档才知道Button的默认style当中有一个属性定义为android:onclickable=”true”.我们需要将这个属性去掉才可以。
想不到还有这个坑。改了,OK,一切正常了。2.还有就是可能没有 window.setFocusable(true);