Android中popupWindow的简单应用

学习Android也有两个多月了,之前一直没有写博客,所以接下来的博客也不会从最开始的那些很基本的控件写起,这次来介绍的是popupWindow。


popupWindow这个类用来实现一个弹出框,可以使用任意布局的View作为其内容,这个弹出框是悬浮在当前activity之上的。因为会弹出一个窗口,所以往往我们也会用到LayoutInflater,刚好可以复习一下上一篇博客的内容,话不多说,见代码。


MainActivity.java

package com.whisker.popupwindowtest;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {
	
	private PopupWindow popupWindow;
	private View view;
	private View titleView;
	private TextView titleTextView;
	private ListView listView;
	private List<String> groups;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        titleView = this.findViewById(R.id.top_title);
        titleTextView = (TextView) titleView.findViewById(R.id.tvTitle);
        titleTextView.setText("Text2");
        titleTextView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View view) {
				Log.i("123", "321");
				showWindow(view);
			}
		});
    }

	private void showWindow(View parent) {
		
		if(popupWindow == null){
			LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			view = layoutInflater.inflate(R.layout.group_list, null);
			listView = (ListView) view.findViewById(R.id.lvGroup);
			
			groups = new ArrayList<String>();
			groups.add("我的微博");
			groups.add("好友");
			groups.add("亲人");
			groups.add("陌生人");
			
			GroupAdapter groupAdapter = new GroupAdapter(this,groups);
			listView.setAdapter(groupAdapter);
			popupWindow = new PopupWindow(view,200,250);
		}
		
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		int xPos = windowManager.getDefaultDisplay().getWidth()/2 - popupWindow.getWidth()/2;
			
		Log.i("whisker", "windowManager.getDefaultDisplay().getWidth()/2:" + windowManager.getDefaultDisplay().getWidth() / 2);
		Log.i("whisker", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);
		Log.i("whisker", "xPos:" + xPos);
			
		popupWindow.showAsDropDown(parent, xPos, 0);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Toast.makeText(MainActivity.this, "你点的是" + groups.get(position), Toast.LENGTH_LONG).show();
				if(popupWindow != null){
					popupWindow.dismiss();
				}
			}
			
		});
		
	}
	

}


GroupAdapter是ListView中不可少的一个类,将ListView中的View和值进行一一对应,也就是我们说的键值对。

GroupAdapter.java

package com.whisker.popupwindowtest;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

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.setText(list.get(position));
		return convertView;
	}
	
	static class ViewHolder{
		TextView groupItem; 
	}
	
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.whisker.popupwindowtest.MainActivity" >

    <include 
        android:id="@+id/top_title"
        layout="@layout/title_two_button"/>

</RelativeLayout>

title_two_button

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    
    <TextView 
        android:id="@+id/tvTitle"
        android:layout_width="match_parent"
        android:layout_height="45dip"
        android:background="@drawable/search_bg"
        android:gravity="center"
        android:text="Title"
        android:textColor="@android:color/black"
        android:textSize="25dip"/>
    
    <Button 
        android:id="@+id/button_left"
        android:layout_width="49dip"
        android:layout_height="36dip"
        android:layout_gravity="center_vertical"
        android:background="@drawable/title_button"/>
    
    <Button 
        android:id="@+id/button_right"
        android:layout_width="49dip"
        android:layout_height="36dip"
        android:layout_gravity="right"
        android:background="@drawable/title_button2"
        android:layout_marginRight="2dip"
        android:layout_marginTop="4dip"/>
    

</FrameLayout>

在这个xml文件中,我们看到两个Button空间中的background属性中指向了“button.xml”和“button2.xml”

这两个xml能够做到Button的动态变化,点击和非点击的不同效果


button.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/title_new_selected"/>
    <item android:drawable="@drawable/title_new_normal"/>

</selector>


button2.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_pressed="true" android:drawable="@drawable/title_reload_selected"/>
    <item android:drawable="@drawable/title_reload_normal"/>

</selector>


我们看到了LayoutInflater指向了group_list这个xml,group_list.xml代码如下:

<?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:orientation="vertical"
    android:background="@drawable/group_bg" >
    
    <TextView 
        android:id="@+id/groupAll"
        android:layout_width="match_parent"
        android:layout_height="40dip"
        android:gravity="center"
        android:background="@drawable/grouplist_fixed_item_bg"
        android:text="All"
        style="@style/grouplist_item_textview" />
    
    <ImageView 
        android:id="@+id/iv_group_list_bg_divider"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/group_divider"
        />
    
    <ListView
        android:id="@+id/lvGroup"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:listSelector="@drawable/grouplist_item_bg_sel"
        android:drawSelectorOnTop="true"
        android:divider="@drawable/group_divider"
        android:cacheColorHint="#000000"
        android:dividerHeight="2.0px"/>
    

</LinearLayout>

我们看到了,这里使用了一个style,是可以用在整个项目中的一个固定格式。

styles
<pre name="code" class="html"><resources xmlns:android="http://schemas.android.com/apk/res/android">

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
    </style>
    
    <style name="grouplist_item_textview">
        <item name="android:textSize">18sp</item>
        <item name="android:textColor">#ffffffff</item>
    </style>

</resources>

group_item_view.xml

 
<pre name="code" class="html"><?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="40dip"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/groupItem"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        style="@style/grouplist_item_textview"/>
    

</LinearLayout>
</pre>groulist_fixed_bg.xml<pre>
<pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/grouplist_item_bg_sel" />
    <item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/grouplist_item_bg_sel" />
    <item android:state_selected="true" android:drawable="@drawable/grouplist_item_bg_sel" />
    <item android:state_focused="true" android:drawable="@drawable/grouplist_item_bg_sel" />

</selector>


这就是整个程序的效果图


 



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值