王立平--PopupWindow

2020-12-13 13:56

阅读:303

标签:popupwindow

soscw.com,搜素材

MainActivity.java


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;
	}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    


王立平--PopupWindow

标签:popupwindow

原文地址:http://blog.csdn.net/u013425527/article/details/40453403


评论


亲,登录后才可以留言!