android PopupWindow的用法

本文介绍了如何在Android应用中利用PopupWindow组件显示任意自定义视图,包括创建PopupWindow实例、设置背景、使内容可聚焦及调整透明度等关键步骤,并通过代码示例展示了一个简单的实现过程。

 一个PopupWindow能显示任意的View。 PopupWindow是漂浮在其他当前Activity之上显示的容器。

demo代码如下:

public class TestPopupWindow extends Activity {
	
    /** Called when the activity is first created. */
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        bt_disPW = (Button) findViewById(R.id.disPW);
        bt_disPW.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				// display popup window
				Log.d("lxy", "at TestActivity button click event");
				
				
				LinearLayout mLayout = new LinearLayout(TestPopupWindow.this);
				TextView tv = new TextView(TestPopupWindow.this);
				tv.setTextColor(Color.BLACK);
				tv.setText("test popup window");
				mLayout.addView(tv);
				
				PopupWindow ppw = new PopupWindow(mLayout, LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
				ppw.setBackgroundDrawable(new ColorDrawable(Color.GRAY));// 设置背景
				ppw.setFocusable(true);
				ppw.getBackground().setAlpha(60);
				
				ppw.showAtLocation(findViewById(R.id.disPW), Gravity.CENTER_VERTICAL, 0, 0);
			}
		});
    }
    
    private Button bt_disPW;
}


main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#FF5555FF"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    <Button
    android:text="DispaleyPopupWindow"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/disPW"></Button>
</LinearLayout>


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值