王立平--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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT兔子123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值