自定义popupwindow组件

 
      
  main.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="500dp"
    android:orientation="vertical" 
    android:background="<a href="http://my.oschina.net/asia" target="_blank" rel="nofollow">@android</a> :color/white"
    >

 <include android:id="@+id/top_title"
     
     layout="@layout/title_two_button"/>
    <Button 
        android:id="@+id/bb"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="button"
        />
</LinearLayout>

title_two_button.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvtitle"
        android:layout_width="fill_parent"
        android:layout_height="45dip"
        android:background="@drawable/search_bg"
        android:gravity="center"
        android:text="天下第一"
        android:textColor="@color/black"
        android:textSize="25dip" />

    <Button
        android:id="@+id/btn_title_left"
        android:layout_width="49dip"
        android:layout_height="36dip"
        android:layout_gravity="center_vertical"
        android:background="@drawable/title_button"
        android:textColor="@color/white" />

    <Button
        android:id="@+id/btn_title_right"
        android:layout_width="49dip"
        android:layout_height="36dip"
        android:layout_gravity="right"
        android:layout_marginRight="2dip"
        android:layout_marginTop="4dip"
        android:background="@drawable/title_button2"
        android:textColor="@color/white" />

</FrameLayout>

  group_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:paddingLeft="0.0sp" android:paddingRight="0.0sp" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_margin="0.0px"
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@drawable/group_bg">

    <ImageView 
        android:id="@+id/iv_group_list_bg_divider" 
        android:background="@drawable/group_divider" 
        android:padding="0.0px" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_margin="0.0px" />
    <ListView 
        android:id="@+id/lvGroup" 
        android:fadingEdgeLength="0.0sp" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        android:listSelector="@drawable/grouplist_item_bg" 
        android:drawSelectorOnTop="true" 
        android:cacheColorHint="#00000000" 
        android:divider="@drawable/group_divider" 
        android:dividerHeight="2.0px" 
        android:layout_weight="0.0" />
</LinearLayout>

  group_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="@dimen/group_item_height"
  xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView 
        android:gravity="center" 
        android:id="@+id/groupItem" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent" 
        style="@style/grouplist_item_textview" />
</LinearLayout>

  

package jk.activity;

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.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

public class PoupWindowDemoActivity extends Activity {

	private PopupWindow popupWindow;

	private ListView lv_group;

	private View view;

	private View top_title;

	private TextView tvtitle;

	private List<String> groups;
	
	private Button button;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		button = (Button)findViewById(R.id.bb);
		top_title = this.findViewById(R.id.top_title);

		tvtitle = (TextView) top_title.findViewById(R.id.tvtitle);

		tvtitle.setText("天下第一");

		tvtitle.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showWindow(v);
			}
		});

	}

	/**
	 * 显示
	 * 
	 * @param parent
	 */
	private void showWindow(View parent) {

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

			view = layoutInflater.inflate(R.layout.group_list, null);

			lv_group = (ListView) view.findViewById(R.id.lvGroup);
			// 加载数据
			groups = new ArrayList<String>();
			groups.add("女儿国");
			groups.add("西游");
			groups.add("大话");
			groups.add("大唐");

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

		// 使其聚集
		popupWindow.setFocusable(true);
		// 设置允许在外点击消失
		popupWindow.setOutsideTouchable(true);

		// 这个是为了点击“返回Back”也能使其消失,并且并不会影响你的背景
		popupWindow.setBackgroundDrawable(new BitmapDrawable());
		WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
		// 显示的位置为:屏幕的宽度的一半-PopupWindow的高度的一半
		int xPos = windowManager.getDefaultDisplay().getWidth() / 2
				- popupWindow.getWidth() / 2;

		Log.i("coder", "windowManager.getDefaultDisplay().getWidth()/2:"
				+ windowManager.getDefaultDisplay().getWidth() / 2);
		//
		Log.i("coder", "popupWindow.getWidth()/2:" + popupWindow.getWidth() / 2);

		Log.i("coder", "xPos:" + xPos);

		popupWindow.showAsDropDown(parent, xPos, 0);

		lv_group.setOnItemClickListener(new OnItemClickListener() {

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

				Toast.makeText(PoupWindowDemoActivity.this,
						"groups.get(position)" + groups.get(position), 1000)
						.show();

				if (popupWindow != null) {
					popupWindow.dismiss();
				}
			}
		});
	}
}

  

package jk.activity;

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;
	}

}

  

转载于:https://www.cnblogs.com/qq6195mc/p/3458331.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值