Android弹出窗口的实现(PopupWindow)

         

                 PopupWindow就是弹出窗口的意思,类似windows下面的开始按钮。PopupWindow可以实现浮层效果,而且可以自定义显示位置,出现和退出时的动画.今天写了一个效果,希望可以帮助到大家.

           下面我们来看看效果图吧:




     1.因为我的项目里的主函数的代码太多啦,就不写出来了,首先我们来要监听事件里面的方法:

   

<span style="font-size:18px;">                        showWindow(v);//调用弹框</span>

        2.方法里面的逻辑:

  

<span style="font-size:18px;">//PopupWindow方法
	private void showWindow(View parent) {  

		if (popupWindow == null) {  
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(HomeActivity.LAYOUT_INFLATER_SERVICE);  

			view = layoutInflater.inflate(R.layout.group_list, null);  
			tv_group = (TextView) view.findViewById(R.id.tv_group);//分组按钮
			
			lv_group = (ListView) view.findViewById(R.id.lvGroup);  
			// 加载数据  
			groups = new ArrayList<String>();  
			groups.add("全部");  
			groups.add("我的微博");  
			groups.add("好友");  
			groups.add("亲人");  
			groups.add("同学");  
			groups.add("朋友");  
			groups.add("陌生人");
			groups.add("陌生人2");
			groups.add("陌生人3");
			groups.add("陌生人4");
			groups.add("陌生人5");
			groups.add("陌生人6");

			GroupAdapter groupAdapter = new GroupAdapter(this, groups);  
			lv_group.setAdapter(groupAdapter);  
			// 创建一个PopuWidow对象  
			popupWindow = new PopupWindow(view, 300, 500);  

		}  

		// 使其聚集  
		popupWindow.setFocusable(true);  
		// 设置允许在外点击消失  
		popupWindow.setOutsideTouchable(true);  
		popupWindow.showAsDropDown(title_tv);//设置在某个控件的下面
		// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景  
		popupWindow.setBackgroundDrawable(new BitmapDrawable());  
		WindowManager windowManager = (WindowManager) getSystemService(HomeActivity.WINDOW_SERVICE);  
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半  
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2  
				- popupWindow.getWidth() / 2;  

		popupWindow.showAsDropDown(parent, xPos, 0);  
		
		tv_group.setOnClickListener(new OnClickListener() {//分组监听
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Toast.makeText(HomeActivity.this, "分组还未开发", Toast.LENGTH_LONG).show();
				if (popupWindow != null) {  
					popupWindow.dismiss();  
				}  
			}
		});
		
		lv_group.setOnItemClickListener(new OnItemClickListener() {  

			@Override  
			public void onItemClick(AdapterView<?> adapterView, View view,  
					int position, long id) {  

				Toast.makeText(HomeActivity.this,  
						groups.get(position), 1000)  
						.show();  

				if (popupWindow != null) {  
					popupWindow.dismiss();  
				}  
			}  
		});  
	}  </span>


 3.方法和逻辑都看完了,我们来看看弹框的布局文件吧:group_list.xml


<span style="font-size:18px;"><?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="@drawable/linearlayout_blue_beijing"
    android:orientation="vertical" >

    <!--
         <ImageView  
        android:id="@+id/iv_group_list_bg_divider"  
        android:layout_width="fill_parent"  
        android:layout_height="20dp"  
        android:background="@drawable/shang"
         />
    -->

    <ListView
        android:id="@+id/lvGroup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerHeight="1dp"
        android:layout_weight="9"
        android:divider="#c0c0c0" />
    
    <TextView android:layout_width="match_parent" 
        android:layout_height="60dp"
        android:id="@+id/tv_group"
        android:text="KING分组"
        android:textColor="#000000"
        android:textSize="18dp" 
        android:gravity="center"
        android:layout_weight="1"/>
   
</LinearLayout></span>


4.适配器的代码是这样的:GroupAdapter.class


<span style="font-size:18px;">import java.util.List;

import com.huihai.home2school.android.depends.main.R;

import android.content.Context;  
import android.graphics.Color;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.TextView;  
  
/**
 * PopupWindow的适配器
 * @author Administrator
 *
 */
public class GroupAdapter extends BaseAdapter {  
  
    private Context context;  
  
    private List<String> list;  
  
    public GroupAdapter(Context context, List<String> list) {  
  
        this.context = context;  
        this.list = list;  
  
    }  
  
    @Override  
    public int getCount() {  
        return list.size();  
    }  
  
    @Override  
    public Object getItem(int position) {  
  
        return list.get(position);  
    }  
  
    @Override  
    public long getItemId(int position) {  
        return position;  
    }  
  
    @Override  
    public View getView(int position, View convertView, ViewGroup viewGroup) {  
  
          
        ViewHolder holder;  
        if (convertView==null) {  
            convertView=LayoutInflater.from(context).inflate(R.layout.group_item_view, null);  
            holder=new ViewHolder();  
              
            convertView.setTag(holder);  
              
            holder.groupItem=(TextView) convertView.findViewById(R.id.groupItem);  
              
        }  
        else{  
            holder=(ViewHolder) convertView.getTag();  
        }  
        holder.groupItem.setTextColor(Color.BLACK);  
        holder.groupItem.setText(list.get(position));  
          
        return convertView;  
    }  
  
    static class ViewHolder {  
        TextView groupItem;  
    }  
  
}  
</span>


5.适配器的布局文件:group_item_view.xml


<span style="font-size:18px;"><?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="50dp"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/groupItem"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:gravity="center"
        android:text="KING"
        android:textColor="#000000"
        android:textSize="18dp" />

</LinearLayout></span>

6.这个是我写在drawable里的倒圆角背景和颜色:linearlayout_blue_beijing.xml


<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <solid android:color="#0099cc" />
    <stroke
        android:width="0.01dp"
        android:color="#0099cc" />
    <corners android:topLeftRadius="10dp"
        android:topRightRadius="10dp"
        android:bottomRightRadius="10dp"
        android:bottomLeftRadius="10dp"/>
</shape></span>

                                                             好了,到这里就完成,有空试试吧



  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值