[java] view plain copy
print
?
- public class PopUpActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this);
- // 引入窗口配置文件
- null);
- // 创建PopupWindow对象
- final PopupWindow pop = new PopupWindow(view, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, false);
- Button btn = (Button) findViewById(R.id.btn);
- // 需要设置一下此参数,点击外边可消失
- new BitmapDrawable());
- //设置点击窗口外边窗口消失
- true);
- // 设置此参数获得焦点,否则无法点击
- true);
- new OnClickListener() {
- @Override
- public void onClick(View v) {
- if(pop.isShowing()) {
- // 隐藏窗口,如果设置了点击窗口外小时即不需要此方式隐藏
- pop.dismiss();
- else {
- // 显示窗口
- pop.showAsDropDown(v);
- }
- }
- });
- }
- }
main.xml
[java] view plain copy
print
?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- "fill_parent"
- "fill_parent"
- "vertical" >
- <Button
- "@+id/btn"
- "wrap_content"
- "wrap_content"
- "dianji" />
- </LinearLayout>
main2.xml
[java] view plain copy
print
?
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- "fill_parent"
- "fill_parent"
- "vertical" >
- <Button
- "@+id/a"
- "wrap_content"
- "wrap_content"
- "AAAAA" />
- <Button
- "wrap_content"
- "wrap_content"
- "BBBBB" />
- <Button
- "wrap_content"
- "wrap_content"
- "CCCCC" />
- <Button
- "wrap_content"
- "wrap_content"
- "DDDDD" />
- </LinearLayout>