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