王立平--PopupWindow

MainActivity.java


<span style="font-size:14px;">package com.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
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;

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.activity_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.ic_launcher));
//		window.setWidth(DisplayManager.dipToPixel(150));
//		window.setHeight(DisplayManager.dipToPixel(150));
		window.setWidth(150);
		window.setHeight(150);


		// 设置PopupWindow外部区域是否可触摸
		window.setFocusable(true); // 设置PopupWindow可获得焦点
		window.setTouchable(true); // 设置PopupWindow可触摸
		window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
		return window;
	}
}</span>
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

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

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

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

</LinearLayout></span>


原文地址:https://www.cnblogs.com/zfyouxi/p/5145806.html