大家好,又见面了,我是全栈君。
Popupwindow 完美demo实现
图示:
关键代码说明:
1.弹出popupwindow,背景变暗
ColorDrawable cd = new ColorDrawable(0x000000); popuWindow1.setBackgroundDrawable(cd); WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp);
2.popupwindow消失之后,背景恢复
public void onDismiss(){ WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); }
3.popupwindow圆角矩形
rounded_corners_bg.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"/> </shape>
pupopwindow布局文件里设置背景
<?xml version="1.0" encoding="UTF-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/rounded_corners_bg" android:orientation="vertical" > ... ... ... </RelativeLayout >
4.点击popupwindow外部。popupwindow也会dismiss
popuWindow1.setOutsideTouchable(true); popuWindow1.setFocusable(true);
完整代码:
1.MainActivity.java
package com.popupwindowdemo; import android.app.Activity; import android.content.Intent; import android.graphics.drawable.ColorDrawable; import android.net.Uri; import android.os.Bundle; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.Button; import android.widget.PopupWindow; import android.widget.TextView; import android.widget.PopupWindow.OnDismissListener; public class MainActivity extends Activity { //popupwindow private PopupWindow popuWindow1; private View contentView1; private TextView textview1; private Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textview1 = (TextView)findViewById(R.id.textView2); btn1 = (Button)findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { initPopuWindow1(v); } }); textview1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://blog.csdn.net/geeklei/article/")); startActivity(intent); } }); } private void initPopuWindow1(View parent) { if (popuWindow1 == null) { LayoutInflater mLayoutInflater = LayoutInflater.from(this); contentView1 = mLayoutInflater.inflate(R.layout.popuwindow1, null); popuWindow1 = new PopupWindow(contentView1,ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); } ColorDrawable cd = new ColorDrawable(0x000000); popuWindow1.setBackgroundDrawable(cd); //产生背景变暗效果 WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 0.4f; getWindow().setAttributes(lp); popuWindow1.setOutsideTouchable(true); popuWindow1.setFocusable(true); popuWindow1.showAtLocation((View)parent.getParent(), Gravity.CENTER|Gravity.CENTER_HORIZONTAL, 0, 0); popuWindow1.update(); popuWindow1.setOnDismissListener(new OnDismissListener(){ //在dismiss中恢复透明度 public void onDismiss(){ WindowManager.LayoutParams lp=getWindow().getAttributes(); lp.alpha = 1f; getWindow().setAttributes(lp); } }); } }
2.activity_main.xml
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.popupwindowdemo.MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_marginTop="23dp" android:text="click to visit my csdn blog" android:textColor="#1C86EE" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView2" android:layout_centerHorizontal="true" android:layout_marginTop="103dp" android:text="click to show popupwindow" /> </RelativeLayout>
3.popupwindow1.xml
<?xml version="1.0" encoding="UTF-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/rounded_corners_bg" android:orientation="vertical" > <LinearLayout android:layout_width="220dp" android:layout_height="wrap_content" android:layout_centerInParent="true" android:layout_gravity="center_vertical|center" android:orientation="vertical" > <TextView android:id="@+id/group_menu1_passfile" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="item1" android:textColor="#000000" android:textSize="13sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#999999" /> <TextView android:id="@+id/group_menu1_playgame" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="item2" android:textColor="#000000" android:textSize="13sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#999999" /> <TextView android:id="@+id/group_menu1_removefriend" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="item3" android:textColor="#000000" android:textSize="13sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#999999" /> <TextView android:id="@+id/group_menu1_setremark" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="item4" android:textColor="#000000" android:textSize="13sp" /> <View android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#999999" /> <TextView android:id="@+id/group_menu1_brokemate" android:layout_width="fill_parent" android:layout_height="40dp" android:layout_marginLeft="20dp" android:gravity="center_vertical" android:text="item5" android:textColor="#000000" android:textSize="13sp" /> </LinearLayout> </RelativeLayout>
4.rounded_corners_bg.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF" /> <corners android:topLeftRadius="5dp" android:topRightRadius="5dp" android:bottomRightRadius="5dp" android:bottomLeftRadius="5dp"/> </shape>
demo下载地址:
http://download.csdn.net/detail/geeklei/7991521
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/116508.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...