弹出PopuoWindow后 代码里设置的是PopupWindow默认获取焦点 所以PopupWindow显示的时候其它控件点击是没有反应的

用到的方法是

pwMyPopWindow.setFocusable(true);

代码里还设置了

pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
R.mipmap.ic_launcher));// 设置背景图片,不能在布局中设置,要通过代码来设置
pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功
这样点击PopupWindow外部就会自行消失 当我们弹出PopupWindow的时候让背景变半透明 当PopuuWindow消失时让背景变回原样 这样就实现了多数app中PouupWindow的使用效果
用到的方法是
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp); 下面是代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#ffffff" > <LinearLayout
android:id="@+id/ll_head_bar"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:orientation="vertical">
<!--这个图片按钮按下时弹出PopupWindow-->
<ImageButton
android:id="@+id/ib_operate_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher" />
<!--这个按钮是测试当PoupWindow显示时点击此按钮没反应 因为此按钮没获取焦点-->
<Button
android:layout_marginTop="200dp"
android:layout_gravity="bottom"
android:id="@+id/ib_operate_more2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="测试按钮" />
</LinearLayout> </RelativeLayout>

<?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="match_parent"
android:gravity="center"
android:orientation="vertical" > <TextView
android:id="@+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:minHeight="40dp"
android:minWidth="120dp"
android:textSize="20sp"
/>
</LinearLayout>

list_item_popupwindow.xml

<?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:orientation="vertical"
android:background="#d93c58b3"
>" <ListView
android:id="@+id/lv_popup_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="#00000000"
android:focusableInTouchMode="true"
android:background="#d42a2a"
/> </LinearLayout>

task_detail_popupwindow.xml

package com.ceshi.popupwindow;

import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.Toast; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; public class MainActivity extends Activity { private ImageButton ibOperationMore;
private Button ibOperationMore2; List<Map<String, String>> moreList;
private PopupWindow pwMyPopWindow;// popupwindow
private ListView lvPopupList;// popupwindow中的ListView
private int NUM_OF_VISIBLE_LIST_ROWS = 3;// 指定popupwindow中Item的数量 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); iniData(); iniPopupWindow(); // 更多操作按钮
ibOperationMore = (ImageButton) findViewById(R.id.ib_operate_more);
//ibOperationMore2是用来测试焦点问题 这样popupwindow显示的时候其他的控件是不能点击的
//换句话说其它控件点击时没反应
ibOperationMore2 = (Button) findViewById(R.id.ib_operate_more2);
ibOperationMore2.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"cao",Toast.LENGTH_SHORT).show();
} }); ibOperationMore.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { if (pwMyPopWindow.isShowing()) {
pwMyPopWindow.dismiss();// 关闭
} else {
pwMyPopWindow.showAsDropDown(ibOperationMore);// 显示
backgroundAlpha(0.7f);
} }
});
} private void iniData() { moreList = new ArrayList<Map<String, String>>();
Map<String, String> map;
map = new HashMap<String, String>();
map.put("share_key", "复制");
moreList.add(map);
map = new HashMap<String, String>();
map.put("share_key", "删除");
moreList.add(map);
map = new HashMap<String, String>();
map.put("share_key", "修改");
moreList.add(map);
} private void iniPopupWindow() { LayoutInflater inflater = (LayoutInflater) this
.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.task_detail_popupwindow, null);
lvPopupList = (ListView) layout.findViewById(R.id.lv_popup_list);
pwMyPopWindow = new PopupWindow(layout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
pwMyPopWindow.setFocusable(true);// 加上这个popupwindow中的ListView才可以接收点击事件 lvPopupList.setAdapter(new SimpleAdapter(MainActivity.this, moreList,
R.layout.list_item_popupwindow, new String[] { "share_key" },
new int[] { R.id.tv_list_item }));
lvPopupList.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
pwMyPopWindow.dismiss();
Toast.makeText(MainActivity.this,
moreList.get(position).get("share_key"),
Toast.LENGTH_LONG).show();
}
}); // 控制popupwindow的宽度和高度自适应
/*lvPopupList.measure(View.MeasureSpec.UNSPECIFIED,
View.MeasureSpec.UNSPECIFIED);
pwMyPopWindow.setWidth(lvPopupList.getMeasuredWidth());
pwMyPopWindow.setHeight((lvPopupList.getMeasuredHeight() + 20)
* NUM_OF_VISIBLE_LIST_ROWS);*/ // 控制popupwindow点击屏幕其他地方消失
pwMyPopWindow.setBackgroundDrawable(this.getResources().getDrawable(
R.mipmap.ic_launcher));// 设置背景图片,不能在布局中设置,要通过代码来设置
pwMyPopWindow.setOutsideTouchable(true);// 触摸popupwindow外部,popupwindow消失。这个要求你的popupwindow要有背景图片才可以成功,如上
pwMyPopWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
@Override
public void onDismiss() {
//popupwindow消失的时候恢复成原来的透明度
backgroundAlpha(1f);
}
}); } /**
* 设置添加屏幕的背景透明度
* @param bgAlpha
*/
public void backgroundAlpha(float bgAlpha)
{
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.alpha = bgAlpha; //0.0-1.0
getWindow().setAttributes(lp);
}
}

MainActivity

效果图



严禁盗版    

转载请注明出处:https://www.cnblogs.com/bimingcong/p/4982237.html

 

弹出PopupWindow背景变暗的实现的更多相关文章

  1. 简单 JS 弹出层 背景变暗

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. PopupWindow 弹出时背景变暗

    下面的PopupWindow  的高是相对于屏幕高设计,宽是获取的某一个控件的宽设置,位置位于某控件的上方,红色部分是设置弹出时屏幕变暗的. //设置contentView View contentV ...

  3. 弹出popwindow 背景变暗

    先看下效果图吧 代码如下 package com.example.administrator.popwindowdemo.view; import android.app.Activity; impo ...

  4. 仿QQ空间根据位置弹出PopupWindow显示更多操作效果

    我们打开QQ空间的时候有个箭头按钮点击之后弹出PopupWindow会根据位置的变化显示在箭头的上方还是下方,比普通的PopupWindow弹在屏幕中间显示好看的多. 先看QQ空间效果图:       ...

  5. 仿iOS底部弹出popUpWindow

    上面为弹出来的效果 popUpWindow布局: <?xml version="1.0" encoding="utf-8"?> <Linear ...

  6. Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

    这是一张QQ空间说说详情的截图. 分析: .点击右上角三个点的图标,在界面底部弹出一个区域,这个区域有一些按钮提供给我们操作 .当该区域出现的时候,详情界面便灰了,也说成透明度变化了 .当任意选了一个 ...

  7. (转载)Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow

    Android项目实战(十七):QQ空间实现(二)—— 分享功能 / 弹出PopupWindow   这是一张QQ空间说说详情的截图. 分析: 1.点击右上角三个点的图标,在界面底部弹出一个区域,这个 ...

  8. PopupWindow-----点击弹出 PopupWindow 初始化菜单

    /** * 点击弹出 PopupWindow 初始化菜单 */ private void initPopupWindow() { PopupWindowAdapter adapter = new Po ...

  9. PopupWindowFromBottom 从底部弹出popupwindow

    自定义PopupWindowFromBottom public class PopupWindowFromBottom extends PopupWindow { public PopupWindow ...

随机推荐

  1. Delphi FrieDAC 大数据处理

    Delphi FrieDAC 大数据处理 大数据处理, 要用到Array DML 插入数据 先要设置插入的数据量 FQuery1.Params.ArraySize := 1000; for index ...

  2. UNITY Profiler 真机调试

    1,WIFI方式, 2,ADB方式WIFI方式:数据通过WIFI在手机和电脑间传输,且手机和电脑必须在一个网段ADB方式如下:数据通过数据线在手机和电脑间传输.显然ADB方式速度快,无网段限制,比较好 ...

  3. Kubernetes 本地仓库

    1.Kubernetes本地私有仓库 Docker仓库主要用于存放Docker镜像,Docker仓库分为公共仓库和私有仓库,基于registry可以搭建本地私有仓库.使用私有仓库有如下优点: 1)节省 ...

  4. Java中的IO流(一)

    一,IO流的分类 A,按照操作的对象不同可分为字节流与字符流 B,按照流的方向不同可分为输入(指从外部存储设备拿文件到内存)流也叫读与输出(指从内存把文件写入到外部存储设备)流也叫写 注:字节流可以操 ...

  5. 递归算法结合数据库 解析 java树形结构

    1.准备表结构及对应的表数据a.表结构: create table TB_TREE ( CID NUMBER not null, CNAME VARCHAR2(50), PID NUMBER //父节 ...

  6. Java计算计算活了多少天

    Java计算计算活了多少天 思路: 1.输入你的出现日期: 2.利用日期转换,将字符串转换成date类型 3.然后将date时间换成毫秒时间 4.然后获取当前毫秒时间: 5.最后计算出来到这个时间多少 ...

  7. Promise.then

    [Promise.then] 1.If onFulfilled returns a promise, the return value of then will be resolved/rejecte ...

  8. metasploit framework(十五):弱点扫描

    openvas扫描生成NBE格式的日志 改个比较好记的文件名 将日志导入到msf进行后续操作,导入之前查看一下hosts和services 导入nbe格式的文件 查看漏洞弱点 msf直接调用nessu ...

  9. metasploit framework(十二):sql server扫描

    mssql_ping模块,如果1433端口开放的话,直接连接这个1433端口,如果没开放,就通过UDP的1434去查询TCP的sql server端口 run,就扫描出来了数据库的tcp端口 得到tc ...

  10. https方式下 git push 每次都要输入密码的解决办法

    转载自:http://git.oschina.net/oschina/git-osc/issues/2586   作者:Zoker https方式每次都要输入密码,按照如下设置即可输入一次就不用再手输 ...