你的位置:首页 > 操作系统

[操作系统]PopupWindow的简单使用


 

测试代码:

package com.zzw.testpopuwindows;import android.app.Activity;import android.graphics.Color;import android.os.Bundle;import android.view.Gravity;import android.view.LayoutInflater;import android.view.View;import android.widget.Button;import android.widget.PopupWindow;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    final Button button = (Button) findViewById(R.id.button);    button.setOnClickListener(new View.OnClickListener() {      @Override      public void onClick(View v) {        LayoutInflater inflater = MainActivity.this.getLayoutInflater();        View view = inflater.inflate(R.layout.item, null);        PopupWindow mPopupWindow = new PopupWindow(view, 500, 300);        //设置背景的目的是使setOutsideTouchable方法生效        mPopupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_launcher));        mPopupWindow.setOutsideTouchable(true);//在外点击消失        // mPopupWindow.showAsDropDown(button);        mPopupWindow.showAtLocation(MainActivity.this.getWindow().getDecorView(), Gravity.CENTER, 0, 0);      }    });  }}

 

item.

<??><LinearLayout ="http://schemas.android.com/apk/res/android"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:background="@android:color/darker_gray"  android:orientation="vertical" >  <ImageView    android:layout_gravity="center"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:src="@drawable/ic_launcher" />  <TextView    android:layout_gravity="center"    android:gravity="center"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="PopupWindow显示"    android:textColor="@android:color/holo_red_light" /></LinearLayout>