package com.example.popupwindow; |
import android.os.Bundle; |
import android.app.Activity; |
import android.graphics.Color; |
import android.graphics.drawable.ColorDrawable; |
import android.view.Gravity; |
import android.view.Menu; |
import android.view.View; |
import android.widget.PopupWindow; |
import android.widget.RelativeLayout; |
public class MainActivity extends Activity { |
private RelativeLayout r; |
PopupWindow p= null ; |
protected void onCreate(Bundle savedInstanceState) { |
super .onCreate(savedInstanceState); |
setContentView(R.layout.activity_main); |
r=(RelativeLayout) findViewById(R.id.r); |
//将xml文件转为view视图 |
View view=View.inflate(MainActivity. this ,R.layout.popupwindow, null ); |
//创建popupwindow对象 |
p= new PopupWindow(view, 500 , 700 , true ); |
//设置是否支持区域外点击消失 |
p.setOutsideTouchable( true ); |
//必须在代码中设置背景颜色(xml不管用),没有背景颜色 |
//即使点击外区域也不会生效 |
p.setBackgroundDrawable( new ColorDrawable(Color.GREEN)); |
} |
public void tiao(View v){ |
//判断是否点击按钮出现 |
if (p.isShowing()){ |
//出现则消失 |
p.dismiss(); |
} else { |
//没有则出现 |
p.showAtLocation(r, Gravity.BOTTOM|Gravity.LEFT, 0 , 0 ); |
} |
|
} |
} |
by: 发表于:2017-10-18 09:19:39 顶(0) | 踩(0) 回复
??
回复评论