android popupwindow位置
来源:互联网 发布:淘宝放单群 编辑:程序博客网 时间:2024/05/17 06:09
因为要做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; }}基本主要的是位置的原点坐标,知道这个剩下的就好处理了
0 0
- android PopupWindow显示位置
- android PopupWindow显示位置
- android popupwindow位置
- android PopupWindow显示位置设置
- android PopupWindow显示位置设置
- android PopupWindow显示位置设置
- Android PopupWindow 的显示位置
- Android PopupWindow 的显示位置
- Android 7.0 popupwindow位置问题
- Android中PopupWindow显示在指定位置
- Android PopupWindow显示位置和显示大小
- Android PopupWindow显示位置和显示大小
- Android UI设计--PopupWindow显示位置设置
- Android中PopupWindow显示在指定位置
- Android popupwindow 弹出的位置问题
- Android应用之PopupWindow显示位置详解
- android 虚拟键适配PopupWindow显示位置
- android 虚拟键适配PopupWindow显示位置
- MyBatis和Hibernate相比
- Android面试经过
- Android图片的三级缓存机制之从网络中获取图片
- Java线程唤醒与阻塞
- BZOJ1178: [Apio2009]CONVENTION会议中心
- android popupwindow位置
- PTA 素数对猜想
- C#委托和事件详解
- HTML——1(HTML 5 文件的基本语法)
- 搭建一个简单的Spring+SpringMVC+JPA+MAVEN 的web项目
- iOS 在分类中添加属性
- OpenCv学习笔记(八)---细说HighGUI(图像用户界面)之滑动条createTrackbar()详解
- 可以改善mysql性能的InnoDB配置参数
- 漫步云中网络