原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://johnson008.blog.51cto.com/4000361/754356
夜未眠,popwindow很好用一种控件,实现一些自定义效果很方便。要想在不同屏幕尺寸的手机上显示的话,就要在代码中设置dip的长度值,很多人都会在代码中用方法转换
//
px转换成dip
public int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return ( int)(dipValue * scale + 0.5f);
}
public int dip2px(Context context, float dipValue){
final float scale = context.getResources().getDisplayMetrics().density;
return ( int)(dipValue * scale + 0.5f);
}
但是有很多机器偏差很大,其实android资源文件中还有dimens.xml这种资源文件。可能这个dimens.xml大家不经常用,它和
strings.xml一样,只不过strings.xml放置字符串而
dimens.xml
放置长度值。dimens引用方法 getResources().getDimension(R.dimen.pop_y);在代码中引用dip,sp.....类型不会变,也不要转换,很方便。代码已经整理打包;
package com.johnson.popwindow;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class PopwindowActivity extends Activity {
private ImageView arrow;
private boolean isOpenPop = false;
private PopupWindow window;
private ListView list;
private LinearLayout title_layout;
public static final String KEY = "key";
ArrayList<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext= this;
arrow = (ImageView) findViewById(R.id.arrow);
title_layout = (LinearLayout) findViewById(R.id.title_layout);
title_layout.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
changPopState(v);
}
});
}
/**
* 更改Pop状态
* */
public void changPopState(View v) {
isOpenPop = !isOpenPop;
if (isOpenPop) {
arrow.setBackgroundResource(R.drawable.icon_arrow_up);
popAwindow(v);
} else {
arrow.setBackgroundResource(R.drawable.icon_arrow_down);
if (window != null) {
window.dismiss();
}
}
}
private void popAwindow(View parent) {
if (window == null) {
LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lay.inflate(R.layout.pop, null);
list = (ListView) v.findViewById(R.id.pop_list);
SimpleAdapter adapter = new SimpleAdapter( this, CreateData(),
R.layout.pop_list_item, new String[] { KEY },
new int[] { R.id.title });
list.setAdapter(adapter);
list.setItemsCanFocus( false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listClickListener);
// window = new PopupWindow(v, 260, 300);
int x = ( int) getResources().getDimension(R.dimen.pop_x);
int y = ( int) getResources().getDimension(R.dimen.pop_y);
window = new PopupWindow(v, x, y);
}
window.setBackgroundDrawable(getResources().getDrawable(
R.drawable.pop_bg));
window.setFocusable( true);
window.setOutsideTouchable( false);
window.setOnDismissListener( new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
isOpenPop = false;
arrow.setBackgroundResource(R.drawable.icon_arrow_down);
}
});
window.update();
window.showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.TOP,
0, ( int) getResources().getDimension(R.dimen.pop_layout_y));
}
OnItemClickListener listClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String, Object> map=(Map<String, Object>) parent.getItemAtPosition(position);
Toast.makeText(mContext,map.get(KEY)+"", Toast.LENGTH_SHORT).show();
if (window != null) {
window.dismiss();
}
}
};
public ArrayList<Map<String, Object>> CreateData() {
Map<String, Object> map;
map = new HashMap<String, Object>();
map.put(KEY, "锅包肉");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "一品酱骨");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "香辣小河虾");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "川白肉");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "莼菜汤");
items.add(map);
return items;
}
}
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class PopwindowActivity extends Activity {
private ImageView arrow;
private boolean isOpenPop = false;
private PopupWindow window;
private ListView list;
private LinearLayout title_layout;
public static final String KEY = "key";
ArrayList<Map<String, Object>> items = new ArrayList<Map<String, Object>>();
Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mContext= this;
arrow = (ImageView) findViewById(R.id.arrow);
title_layout = (LinearLayout) findViewById(R.id.title_layout);
title_layout.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
changPopState(v);
}
});
}
/**
* 更改Pop状态
* */
public void changPopState(View v) {
isOpenPop = !isOpenPop;
if (isOpenPop) {
arrow.setBackgroundResource(R.drawable.icon_arrow_up);
popAwindow(v);
} else {
arrow.setBackgroundResource(R.drawable.icon_arrow_down);
if (window != null) {
window.dismiss();
}
}
}
private void popAwindow(View parent) {
if (window == null) {
LayoutInflater lay = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = lay.inflate(R.layout.pop, null);
list = (ListView) v.findViewById(R.id.pop_list);
SimpleAdapter adapter = new SimpleAdapter( this, CreateData(),
R.layout.pop_list_item, new String[] { KEY },
new int[] { R.id.title });
list.setAdapter(adapter);
list.setItemsCanFocus( false);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setOnItemClickListener(listClickListener);
// window = new PopupWindow(v, 260, 300);
int x = ( int) getResources().getDimension(R.dimen.pop_x);
int y = ( int) getResources().getDimension(R.dimen.pop_y);
window = new PopupWindow(v, x, y);
}
window.setBackgroundDrawable(getResources().getDrawable(
R.drawable.pop_bg));
window.setFocusable( true);
window.setOutsideTouchable( false);
window.setOnDismissListener( new OnDismissListener() {
@Override
public void onDismiss() {
// TODO Auto-generated method stub
isOpenPop = false;
arrow.setBackgroundResource(R.drawable.icon_arrow_down);
}
});
window.update();
window.showAtLocation(parent, Gravity.CENTER_HORIZONTAL | Gravity.TOP,
0, ( int) getResources().getDimension(R.dimen.pop_layout_y));
}
OnItemClickListener listClickListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Map<String, Object> map=(Map<String, Object>) parent.getItemAtPosition(position);
Toast.makeText(mContext,map.get(KEY)+"", Toast.LENGTH_SHORT).show();
if (window != null) {
window.dismiss();
}
}
};
public ArrayList<Map<String, Object>> CreateData() {
Map<String, Object> map;
map = new HashMap<String, Object>();
map.put(KEY, "锅包肉");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "一品酱骨");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "香辣小河虾");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "川白肉");
items.add(map);
map = new HashMap<String, Object>();
map.put(KEY, "莼菜汤");
items.add(map);
return items;
}
}
本文出自 “Johnson” 博客,请务必保留此出处http://johnson008.blog.51cto.com/4000361/754356