Android popupwindow 示例程序一

经过多番测试实践,实现了popupwindow 弹出在指定控件的下方。代码上有注释,有需要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。

项目资源下载:点击这里

TestPopwindow2.class

package com.example.popwindowdemo;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;

public class TestPopwindow2 extends PopupWindow {
	// 根视图
	private View mRootView;
	// LayoutInflater
	LayoutInflater mInflater;

	public TestPopwindow2(Activity context) {
		super(context);
		mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);
		setContentView(mRootView);

		this.setWidth(LayoutParams.WRAP_CONTENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);

		// 设置PopUpWindow弹出的相关属性
		setTouchable(true);
		setOutsideTouchable(true);
		setFocusable(true);
		setBackgroundDrawable(new BitmapDrawable(context.getResources()));
		update();

		getContentView().setFocusableInTouchMode(true);
		getContentView().setFocusable(true);
		setAnimationStyle(R.style.AppBaseTheme);
	}
}
MainActivity.class

package com.example.popwindowdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener,
		OnDismissListener {

	private TestPopwindow2 mTestPopwindow2 = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		InitUI();

	}

	private void InitUI() {
		// 实例化TestPopwindow2
		mTestPopwindow2 = new TestPopwindow2(this);
		// 设置点击其他位置mTestPopwindow2消失
		mTestPopwindow2.setOnDismissListener(this);

		Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);
		buttonTest2.setOnClickListener(this);
	}

	private void OnPopwindowTest2() {
		if (mTestPopwindow2 == null)
			return;

		// location获得控件的位置
		int[] location = new int[2];
		View v = findViewById(R.id.buttonTest2);
		if (v != null)
			v.getLocationOnScreen(location);   // 控件在屏幕的位置
		mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme);
		
		// mTestPopwindow2弹出在某控件(button)的下面
		mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,
				location[0] - v.getWidth(), location[1] + v.getHeight());
	}

	// mTestPopwindow2布局控件的监听
	public void OnclickTestListener(View view) {
		switch (view.getId()) {
		case R.id.layoutSeclect1:
			Toast.makeText(this, "系统热门方案", Toast.LENGTH_SHORT).show();
			break;
		case R.id.layoutSeclect2:
			Toast.makeText(this, "个人热门方案", Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
		if (mTestPopwindow2 != null)
			mTestPopwindow2.dismiss();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonTest2:
			OnPopwindowTest2();
			break;
		default:
			break;
		}
	}

	// 点击其他地方消失
	@Override
	public void onDismiss() {
	}
}
test_popwindow_2.xml
<?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:background="#0000" >

    <!--
  popupwimdow的布局注意不能使用类似        
    android:layout_marginTop=""这样属性,特别是相对布局时候. 
  布局或者控件整体靠左等都会影响popupwimdow定位使用
    -->

    <LinearLayout
        android:id="@+id/hot_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/title_function_bg"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/layoutSeclect1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="OnclickTestListener" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="系统热门方案"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layoutSeclect2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:onClick="OnclickTestListener" >

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="8dp"
                android:text="个人热门方案"
                android:textColor="#fff"
                android:textSize="18sp" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>
activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayoutMain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">
        
    <Button
        android:id="@+id/buttonTest2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试2" />
    </LinearLayout>

</LinearLayout>
效果图如下

项目资源下载:点击这里

转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871
如有转载未注明出处博客网址,或者没有得到作者的同意。作者持有版权所有权。



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值