PopupWindow是一种比较好用的控件,通过它来实现一些自定义效果非常简便。如果需要在不同屏幕尺寸的手机上显示时,就有必要在程序中设置dip的长度值。很多人都会在代码中像下面这样转换。
Java代码
- //px转换成dip
- 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.....类型不会变,也不要转换,很方便。下面上代码。
Java代码
- 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;
- }
- }
本文发布:Android开发网
发表评论:
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。