PopouWindow的使用

项目中PopupWindow还是很常见的,用好了效果还不错哈。不显单调。忙晕了,好久没更新博客了,今天先发个Deno来签个到。

1、效果图


2、布局文件

主布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_popupwindow"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:text="点我"
 />

    

</LinearLayout>

PopupWindow布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="130dip"
    android:layout_height="75dip"
    android:paddingLeft="10dip"
    android:background="@drawable/blue_bkg"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_popupwindow_mode1"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:text="模式一"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/tv_popupwindow_mode2"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:text="模式二"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/tv_popupwindow_mode3"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:text="模式三"
        android:textColor="#ffffff" />

    <TextView
        android:id="@+id/tv_popupwindow_mode4"
        android:layout_width="120dip"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:text="模式四"
        android:textColor="#ffffff" />

</LinearLayout>

3、Activity

public class MainActivity extends Activity {

	private PopupWindow mPW;
	private ScaleAnimation mPopupAnimation;
	private Button bt_popupwindow;
	private View view;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		initView();
		initPopupWindow();
		initEvent();
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		setContentView(R.layout.activity_main);
		bt_popupwindow = (Button) findViewById(R.id.bt_popupwindow);
	}

	/**
	 * 初始化点击事件
	 */
	private void initEvent() {
		bt_popupwindow.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 判断窗体是否显示
				if (mPW != null && mPW.isShowing()) {
					// 关闭
					mPW.dismiss();
				} else {
					//控制显示的位置
					mPW.showAsDropDown(v, 45, 0);
					//显示动画
					view.startAnimation(mPopupAnimation);
				}
			}
		});
	}

	/**
	 * 初始化界面
	 */
	private void initPopupWindow() {

		view = View.inflate(getApplicationContext(), R.layout.popupwindow,
				null);
		// 获取子组件,为其添加事件
		TextView tv_mode1 = (TextView) view
				.findViewById(R.id.tv_popupwindow_mode1);
		TextView tv_mode2 = (TextView) view
				.findViewById(R.id.tv_popupwindow_mode2);
		TextView tv_mode3 = (TextView) view
				.findViewById(R.id.tv_popupwindow_mode3);
		TextView tv_mode4 = (TextView) view
				.findViewById(R.id.tv_popupwindow_mode4);

		// 设置共有的点击监听
		OnClickListener mListener = new OnClickListener() {

			@Override
			public void onClick(View v) {
				switch (v.getId()) {
				case R.id.tv_popupwindow_mode1:

					break;
				case R.id.tv_popupwindow_mode2:

					break;
				case R.id.tv_popupwindow_mode3:

					break;
				case R.id.tv_popupwindow_mode4:

					break;

				default:
					break;
				}
				// 关闭对话框
				mPW.dismiss();
			}

		};
		tv_mode1.setOnClickListener(mListener);
		tv_mode2.setOnClickListener(mListener);
		tv_mode3.setOnClickListener(mListener);
		tv_mode4.setOnClickListener(mListener);

		// 初始化弹出窗体,设定大小
		mPW = new PopupWindow(view, 100, 120);

		// 设置参数
		// 设置焦点,保证里面的组件可以点击
		mPW.setFocusable(true);
		// 设置背景,好处:1、外部点击生效 2、可以播放动画
		mPW.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));// 透明
		mPW.setOutsideTouchable(true);

		// 为显自然,加个渐变动画
		mPopupAnimation = new ScaleAnimation(1.0f, 1.0f, 0.0f, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0);
		mPopupAnimation.setDuration(400);
	}

}
就这样愉快地完工了,一切尽在注释中,也没什么技术含量,纯属乐呵乐呵。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值