android popupwindow 仿新浪、腾讯title弹框效果

最近项目中使用到PopupWindow,因此仔细研究了一下PopupWindow的使用方法。也借鉴了网上不少前辈的思想,实现了仿 新浪、腾讯title 弹框效果,跟大家分享一下!


运行效果截图




import android.app.Activity;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

public class MainActivity extends Activity {
	private Button bt_order;
	private PopupWindow popupWindow;
	private LinearLayout layout;
	private ListView listView;
	private String title[] = { "全部", "我的微博", "周边", "智能排版", "同学","密友","系统推荐" };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		bt_order = (Button) findViewById(R.id.bt_order);
		bt_order.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
//				showPopupWindowA();
				showPopupWindowB();
			}
		});
	}
	
	/**
	 * 使用 showAsDropDown 方法显示
	 */
	private void showPopupWindowB() {
		layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
				R.layout.dialog, null);
		listView = (ListView) layout.findViewById(R.id.lv_dialog);
		listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
				R.layout.text, R.id.tv_text, title));

		popupWindow = new PopupWindow(MainActivity.this);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow
		.setWidth(getResources().getDimensionPixelSize(
				R.dimen.mark_popupwindow_width));
		popupWindow.setHeight(getResources().getDimensionPixelSize(
				R.dimen.mark_popupwindow_height));
		
		popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        popupWindow.setTouchable(true); // 设置PopupWindow可触摸
        popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
//        popupWindow.setAnimationStyle(R.anim.popanim);
        
		popupWindow.setContentView(layout);
		
//		popupWindow.showAsDropDown(bt_order);
		popupWindow.showAsDropDown(bt_order, (getResources().getDimensionPixelSize(
				R.dimen.bt_width) - getResources().getDimensionPixelSize(
				R.dimen.mark_popupwindow_width))/2, 0);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				bt_order.setText(title[arg2]);
				popupWindow.dismiss();
				popupWindow = null;
			}
		});
	}

	/**
	 * 使用 showAtLocation 方法显示,copy网上某位博主的实现方式
	 */
	public void showPopupWindowA() {
		Rect frame = new Rect();
        getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int state_heght = frame.top;// 状态栏的高度

        int y = bt_order.getBottom()+state_heght ;
        int x = getWindowManager().getDefaultDisplay().getWidth()/4;
        
		layout = (LinearLayout) LayoutInflater.from(MainActivity.this).inflate(
				R.layout.dialog, null);
		listView = (ListView) layout.findViewById(R.id.lv_dialog);
		listView.setAdapter(new ArrayAdapter<String>(MainActivity.this,
				R.layout.text, R.id.tv_text, title));

		popupWindow = new PopupWindow(MainActivity.this);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		popupWindow
		.setWidth(getResources().getDimensionPixelSize(
				R.dimen.mark_popupwindow_width));
		popupWindow.setHeight(getResources().getDimensionPixelSize(
				R.dimen.mark_popupwindow_height));
		
		popupWindow.setFocusable(true); // 设置PopupWindow可获得焦点
        popupWindow.setTouchable(true); // 设置PopupWindow可触摸
        popupWindow.setOutsideTouchable(false); // 设置非PopupWindow区域可触摸
//        popupWindow.setAnimationStyle(R.anim.popanim);
        
		popupWindow.setContentView(layout);
		// showAsDropDown会把里面的view作为参照物,所以要那满屏幕parent
		// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);
		popupWindow.showAtLocation(findViewById(R.id.main), Gravity.LEFT
				| Gravity.TOP, x, y);//需要指定Gravity,默认情况是center.

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				bt_order.setText(title[arg2]);
				popupWindow.dismiss();
				popupWindow = null;
			}
		});
	}

}



showPopupWindowA 是借鉴网上的一位博主实现的,链接:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=240654。showPopupWindowB是我自己的实现方式。


MainActivity 布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/background_light"
    android:orientation="vertical" >

    <Button
        android:id="@+id/bt_order"
        android:layout_width="@dimen/bt_width"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="排序"
        android:textSize="20sp" />

</LinearLayout>


PopupWindow布局文件

<?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:background="@drawable/click"
    android:cacheColorHint="#00000000"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lv_dialog"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="#00000000"
        android:listSelector="@drawable/grouplist_item_bg_normal" >
    </ListView>

</LinearLayout>



显示 PopupWindow 有 两种方式:一种是showAtLocation,一种是 showAsDropDown,具体介绍可以参看Google Docs。使用showAtLocation方式显示的时候需要计算StatusBar 的高度,在某些机型上有问题。个人觉得使用 showAsDropDown 方法更靠谱一些。



另外如果想 点击 PopupWindow 窗体之外的区域,PopupWindow消失,设置 popupWindow.setOutsideTouchable(true);  即可



工程下载地址:http://download.csdn.net/detail/fx_sky/6285833






评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值