android popupwindow位置
2016-04-26 16:46
519 查看
因为要做recyclerview点击弹窗问题,感觉popupwindow能够比较好的处理焦点和位置问题,来看看位置的处理
activity
package com.fanyafeng.orlitedemo.activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.PopupWindow; import com.fanyafeng.orlitedemo.BaseActivity; import com.fanyafeng.orlitedemo.R; import com.fanyafeng.orlitedemo.util.MyUtils; import java.io.File; import java.util.ArrayList; import java.util.List; public class PopupWindowActivity extends BaseActivity { private Button btn_bottom_pop; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_popup_window); Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); initView(); initData(); } private void initView() { btn_bottom_pop = (Button) findViewById(R.id.btn_bottom_pop); } private void initData() { } private void downShowPopupWindow(View view) { PopupWindow popupWindow; View popView = getLayoutInflater().inflate(R.layout.layout_popwindow_recyclerview, null); popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null)); popupWindow.showAsDropDown(view); } private void bottomShowPopupWindow(View view) { PopupWindow popupWindow; View popView = getLayoutInflater().inflate(R.layout.layout_popwindow_recyclerview, null); popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null)); float popupWindowWidth = MyUtils.dip2px(this, 96); Log.d("TAG", "pop的宽度:" + popupWindowWidth); int offsetX = MyUtils.getScreenWidth(this) / 2; int[] location = new int[2]; btn_bottom_pop.getLocationInWindow(location); int popX = location[0]; int popY = location[1]; //相对位置,子点击的控件和margin开始定为原点(0,0) popupWindow.showAsDropDown(view, offsetX - (int) popupWindowWidth / 2, MyUtils.getScreenHeight(this) - MyUtils.getNavigationBarHeight(this) - (int) MyUtils.dip2px(this, 80) - popY); } private void bottomCenterShowPopupWindow(View view) { PopupWindow popupWindow; View popView = getLayoutInflater().inflate(R.layout.layout_popwindow_recyclerview, null); popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setTouchable(true); popupWindow.setOutsideTouchable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable(getResources(), (Bitmap) null)); popupWindow.showAtLocation(view, Gravity.CENTER, 0, MyUtils.getScreenHeight(this) - MyUtils.getNavigationBarHeight(this)); } @Override public void onClick(View v) { super.onClick(v); switch (v.getId()) { case R.id.btn_down_pop: downShowPopupWindow(v); break; case R.id.btn_bottom_pop: bottomShowPopupWindow(v); break; case R.id.btn_bottom_center_pop: bottomCenterShowPopupWindow(v); break; } } }
再来看用到的工具类myutils
package com.fanyafeng.orlitedemo.util; import android.app.Activity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.res.Resources; import android.graphics.Rect; import android.os.Build; import android.util.DisplayMetrics; import android.view.ViewConfiguration; import android.view.Window; import android.view.WindowManager; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class MyUtils { private static String channel = null; public static String getChannel(Context context) { if (channel == null) { ApplicationInfo ai = null; channel = "shape_maintext_box"; try { ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); if (ai != null) { channel = String.valueOf(ai.metaData.get("UMENG_CHANNEL")); } } catch (Exception e) { e.printStackTrace(); } } return channel; } public static int getStatusBarHeight(Context context) { try { @SuppressWarnings("rawtypes") Class clazz = Class.forName("com.android.internal.R$dimen"); Object object = clazz.newInstance(); Field field = clazz.getField("status_bar_height"); int id = Integer.parseInt(field.get(object).toString()); return context.getResources().getDimensionPixelSize(id); } catch (Exception e) { e.printStackTrace(); } return 0; } /** * Get the screen height. * * @param context * @return the screen height */ public static int getScreenHeight(Context context) { if (context != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.heightPixels; } else { return 1920; } } /** * Get the screen width. * * @param context * @return the screen width */ public static int getScreenWidth(Context context) { if (context != null) { DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.widthPixels; } else { return 1080; } } public static float getDensity(Context context) { DisplayMetrics displayMetrics = new DisplayMetrics(); WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); windowManager.getDefaultDisplay().getMetrics(displayMetrics); return displayMetrics.density; } public static String getMetaValue(Context context, String metaKey) { if (context == null || metaKey == null) { return null; } try { ApplicationInfo aiApplicationInfo = context.getPackageManager().getApplicationInfo( context.getPackageName(), PackageManager.GET_META_DATA); if (null != aiApplicationInfo) { if (null != aiApplicationInfo.metaData) { return aiApplicationInfo.metaData.getString(metaKey); } } } catch (PackageManager.NameNotFoundException e) { e.printStackTrace(); } return null; } /** * 格式化日期 * yyyy-MM-dd 转为 yyyy年MM月dd日 * * @param dateStr * @return */ public static String formatSystemDateCN(String dateStr) { SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd"); try { String standarTime; Date date = simpleDateFormat.parse(dateStr); simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日"); standarTime = simpleDateFormat.format(date); return standarTime; } catch (ParseException e) { e.printStackTrace(); } return dateStr; } public static float dip2px(Context context, float dipValue) { final float scale = context.getResources().getDisplayMetrics().density; return (float) (dipValue * scale + 0.5f); } public static float px2dip(Context context, float pxValue) { final float scale = context.getResources().getDisplayMetrics().density; return (float) (pxValue / scale + 0.5f); } /** * 获取标题栏的高度 * * @param activity * @return */ public int getTitleHeight(Activity activity) { Rect rect = new Rect(); Window window = activity.getWindow(); window.getDecorView().getWindowVisibleDisplayFrame(rect); int statusBarHeight = rect.top; int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); int titleBarHeight = contentViewTop - statusBarHeight; return titleBarHeight; } /** * * 获取状态栏高度 * * @param activity * @return */ public int getStateHeight(Activity activity) { Rect rect = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect); return rect.top; } /** * 获取屏幕宽高 * * @param activity * @return int[0] 宽,int[1]高 */ public int[] getScreenWidthAndSizeInPx(Activity activity) { DisplayMetrics displayMetrics = new DisplayMetrics(); activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); int[] size = new int[2]; size[0] = displayMetrics.widthPixels; size[1] = displayMetrics.heightPixels; return size; } /** * 获取虚拟按键栏高度 * @param context * @return */ public static int getNavigationBarHeight(Context context) { int result = 0; if (hasNavBar(context)){ Resources res = context.getResources(); int resourceId = res.getIdentifier("navigation_bar_height", "dimen", "android"); if (resourceId > 0) { result = res.getDimensionPixelSize(resourceId); } } return result; } /** * 检查是否存在虚拟按键栏 * @param context * @return */ private static boolean hasNavBar(Context context) { Resources res = context.getResources(); int resourceId = res.getIdentifier("config_showNavigationBar", "bool", "android"); if (resourceId != 0) { boolean hasNav = res.getBoolean(resourceId); // check override flag String sNavBarOverride = getNavBarOverride(); if ("1".equals(sNavBarOverride)) { hasNav = false; } else if ("0".equals(sNavBarOverride)) { hasNav = true; } return hasNav; } else { // fallback return !ViewConfiguration.get(context).hasPermanentMenuKey(); } } /** * 判断虚拟按键栏是否重写 * @return */ private static String getNavBarOverride() { String sNavBarOverride = null; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { try { Class c = Class.forName("android.os.SystemProperties"); Method m = c.getDeclaredMethod("get", String.class); m.setAccessible(true); sNavBarOverride = (String) m.invoke(null, "qemu.hw.mainkeys"); } catch (Throwable e) { } } return sNavBarOverride; } }基本主要的是位置的原点坐标,知道这个剩下的就好处理了
相关文章推荐
- android 滚动时间选择器
- 自定义View学习-绘制一个简单的圆
- Android图片压缩的几种方式
- Android SDK目录结构及作用
- android,一般动态注册广播的创建和适用具体方法
- android 批量打包
- Android Layout XML属性
- [Android]手机短信验证功能
- 15 个必知的 Android 开发者选项
- Android与H5数据交互
- Android 6.0运行时权限解决办法
- 新思路_Android同时显示多个跑马灯
- 超实用的Android手势锁制作实例教程
- Android的SharedPreferences和PreferenceActivity用法
- Android this document contains very long lines
- Android成长之路(4)——Activity的生命周期(2)
- Android高可拓展框架Conductor源码解析(2)
- Android横竖屏切换总结(Android资料)
- android studio debug签名设置
- android横竖屏切换时Activity的生命周期