popwindow弹框

public class MainActivity extends Activity {
private Button but;
private Button but1;
private ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but=(Button)findViewById(R.id.but);
but1=(Button)findViewById(R.id.but1);
img=(ImageView)findViewById(R.id.img);

but.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub showPop(v); } }); but1.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub show1(v); } }); } private void showPop(View v){ View pv=View.inflate(this, R.layout.popwindow, null); TextView tv1=(TextView)pv.findViewById(R.id.tv1); final PopupWindow pop=new PopupWindow(pv, LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT); pop.setOutsideTouchable(true); pop.setBackgroundDrawable(new ColorDrawable(0x00000000)); pop.setFocusable(true); tv1.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "拍照", Toast.LENGTH_SHORT).show(); pop.dismiss(); } }); pop.showAtLocation(pv, Gravity.BOTTOM, 0, 0); }private void show1(View v){ View popView=View.inflate(this, R.layout.pop, null); final Button but_zan=(Button)popView.findViewById(R.id.but_zan); popView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); PopupWindow pop=new PopupWindow(popView,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); //点击空白处弹框消失 pop.setOutsideTouchable(true); pop.setBackgroundDrawable(new ColorDrawable(0x00000000)); //点击back键 弹框消失 pop.setFocusable(true); //显示在左下方

【popwindow弹框】// pop.showAsDropDown(v);
//显示在左边 int[] location=new int[2]; //找到button在屏幕上的位置将宽高存入数组location中 v.getLocationOnScreen(location); //获得popview位置 int width=popView.getMeasuredWidth(); //(设置popview的位置相对于父容器)将popview设置button的左边,宽度为button左上角位置坐标减去popview的宽度,高度为button的高度。 but_zan.setOnClickListener(new OnClickListener() {@Override public void onClick(View v) { // TODO Auto-generated method stub String s=but_zan.getText().toString(); if(s.equals("赞")){ but_zan.setText("取消赞"); img.setVisibility(View.VISIBLE); }else{ but_zan.setText("赞"); img.setVisibility(View.GONE); } } }); pop.showAtLocation(v, Gravity.NO_GRAVITY, location[0]-width, location[1]); }

}
博客地址:http://my.csdn.net/?ref=toolbar

    推荐阅读