当前位置: 首页 > >

Android中 Popwindow的使用

发布时间:

1.使用 popwindow实现如下效果:



2.代码如下:MainActivity.java


?





[java]
view plain
copy
print
?


  1. package?mtpop.window.main;????
  2. import?android.app.Activity;??import?android.content.Context;??
  3. import?android.content.res.Resources;??import?android.os.Bundle;??
  4. import?android.view.Gravity;??import?android.view.LayoutInflater;??
  5. import?android.view.View;??import?android.view.View.OnClickListener;??
  6. import?android.view.ViewGroup.LayoutParams;??import?android.widget.Button;??
  7. import?android.widget.LinearLayout;??import?android.widget.PopupWindow;??
  8. import?android.widget.PopupWindow.OnDismissListener;??import?android.widget.TextView;??
  9. ??public?class?MainActivity?extends?Activity??
  10. {????
  11. ????private?static?final?String?TAG?=?"MainActivity";??????private?Button?button;??
  12. ??????@Override??
  13. ????public?void?onCreate(Bundle?savedInstanceState)??????{??
  14. ????????super.onCreate(savedInstanceState);??????????setContentView(R.layout.main);??
  15. ??????????button?=?(Button)?findViewById(R.id.button);??
  16. ????????button.setOnClickListener(new?OnClickListener()??????????{??
  17. ????????????@Override??????????????public?void?onClick(View?v)??
  18. ????????????{??????????????????//?显示?popupWindow ??
  19. ????????????????PopupWindow?popupWindow?=?makePopupWindow(MainActivity.this);??????????????????int[]?xy?=?new?int[2];??
  20. ????????????????button.getLocationOnScreen(xy);??????????????????popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());??
  21. ????????????????//popupWindow.showAsDropDown(button,0,?0); ??????????????}??
  22. ????????});??????}??
  23. ??????//?创建一个包含自定义view的PopupWindow ??
  24. ????private?PopupWindow?makePopupWindow(Context?cx)??????{??
  25. ????????PopupWindow?window;??????????window?=?new?PopupWindow(cx);??
  26. ??????????????????//View?contentView?=?LayoutInflater.from(this).inflate(R.layout.popwindow,?null); ??
  27. ????????//window.setContentView(contentView); ??????????Button?b1?=?new?Button(this);??
  28. ????????b1.setText("first");??????????b1.setLayoutParams(new?LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));??
  29. ??????????????????Button?b2?=?new?Button(this);??
  30. ????????b2.setText("Second");??????????b2.setLayoutParams(new?LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));??
  31. ??????????????????LinearLayout?linearLayout?=?new?LinearLayout(this);??
  32. ????????linearLayout.addView(b1);??????????linearLayout.addView(b2);??
  33. ????????linearLayout.setOrientation(LinearLayout.VERTICAL);????????????
  34. ????????window.setContentView(linearLayout);??????????window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));??
  35. ????????window.setWidth(DisplayManager.dipToPixel(150));??????????window.setHeight(DisplayManager.dipToPixel(150));??
  36. ??????????????????//?设置PopupWindow外部区域是否可触摸 ??
  37. ????????window.setFocusable(true);?//设置PopupWindow可获得焦点 ??????????window.setTouchable(true);?//设置PopupWindow可触摸 ??
  38. ????????window.setOutsideTouchable(true);?//设置非PopupWindow区域可触摸 ??????????return?window;??
  39. ????}??}??


package mtpop.window.main;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.TextView;

public class MainActivity extends Activity
{

private static final String TAG = "MainActivity";
private Button button;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// 显示 popupWindow
PopupWindow popupWindow = makePopupWindow(MainActivity.this);
int[] xy = new int[2];
button.getLocationOnScreen(xy);
popupWindow.showAtLocation(button,Gravity.RIGHT|Gravity.TOP,-xy[0]/2,xy[1]+button.getWidth());
//popupWindow.showAsDropDown(button,0, 0);
}
});
}

// 创建一个包含自定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx)
{
PopupWindow window;
window = new PopupWindow(cx);

//View contentView = LayoutInflater.from(this).inflate(R.layout.popwindow, null);
//window.setContentView(contentView);
Button b1 = new Button(this);
b1.setText("first");
b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

Button b2 = new Button(this);
b2.setText("Second");
b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));

LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(b1);
linearLayout.addView(b2);
linearLayout.setOrientation(LinearLayout.VERTICAL);

window.setContentView(linearLayout);
window.setBackgroundDrawable(getResources().getDrawable(R.drawable.pop_bg));
window.setWidth(DisplayManager.dipToPixel(150));
window.setHeight(DisplayManager.dipToPixel(150));

// 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
return window;
}
}


3. main.xml






[html]
view plain
copy
print
?


  1. ??????android:layout_width="fill_parent"??????android:layout_height="fill_parent"??
  2. ????android:orientation="vertical"?>????
  3. ????????????android:layout_height="wrap_content"??????????android:background="@android:color/darker_gray"??
  4. ????????android:orientation="horizontal"?>????
  5. ????????????????????android:layout_height="wrap_content"??????????????android:layout_weight="1"??
  6. ????????????android:text="Title"?/>????
  7. ????????????????????android:layout_width="wrap_content"??????????????android:layout_height="wrap_content"??
  8. ????????????android:text="click"?/>????????
  9. ????



android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="horizontal" >

android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" />

android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click" />




注意:
???


???? *?新建一个popupWindow弹出框?popupWindow是一个阻塞式的弹出框,这就意味着在我们退出这个弹出框之前,程序会一直等待,????


???? *?这和AlertDialog不同哦,AlertDialog是非阻塞式弹出框,AlertDialog弹出的时候,后台可是还可以做其他事情的哦。???? *



?


******************************************************************


1.PopupWindow的隐藏


??





[java]
view plain
copy
print
?


  1. final?PopupWindow?window?=?mPageStatWin;??if(null?!=?window?&&?window.isShowing())?{??
  2. ????win.dismiss();??}??


final PopupWindow window = mPageStatWin;
if(null != window && window.isShowing()) {
win.dismiss();
}


2.Popupwindow的显示及位置设置


window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10);




第一个参数指定PopupWindow的锚点view,即依附在哪个view上。


第二个参数指定起始点为parent的右下角,第三个参数设置以parent的右下角为原点,向左、上各偏移10像素。




???



//将PopupWindow作为anchor的下拉窗口显示。即在anchor的左下角显示

window.
showAsDropDown
(anchor
)
;


//xoff,yoff基于anchor的左下角进行偏移。

window.
showAsDropDown
(anchor, xoff, yoff
)
;


如果没有充足的空间显示PopupWindow,那么PopupWindow的左下角将位于anchor的左上角来显示。







???????? *?popupWindow.showAsDropDown(View?view)弹出对话框,位置在紧挨着view组件????


? ?? ??? *?showAsDropDown(View?anchor,?int?xoff,?int?yoff)弹出对话框,位置在紧挨着view组件,x?y?代表着偏移量?????


? ? ? ?? *?showAtLocation(View?parent,?int?gravity,?int?x,?int?y)弹出对话框?


?? ?????? *?parent?父布局?gravity?依靠父布局的位置如Gravity.CENTER??x?y?坐标值?????????






[java]
view plain
copy
print
?


  1. window.setWidth(DisplayManager.dipToPixel(150));??window.setHeight(DisplayManager.dipToPixel(150));??
  2. ??//?设置PopupWindow外部区域是否可触摸 ??
  3. window.setFocusable(true);?//设置PopupWindow可获得焦点 ??window.setTouchable(true);?//设置PopupWindow可触摸 ??
  4. window.setOutsideTouchable(true);?//设置非PopupWindow区域可触摸??


window.setWidth(DisplayManager.dipToPixel(150));
window.setHeight(DisplayManager.dipToPixel(150));

// 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); //设置PopupWindow可获得焦点
window.setTouchable(true); //设置PopupWindow可触摸
window.setOutsideTouchable(true); //设置非PopupWindow区域可触摸
???????
设置其可以获得焦点,可触摸,外部区域可触摸(很重要)





友情链接: 时尚网 总结汇报 幼儿教育 小学教育 初中学习资料网