android实现筛选菜单效果
作者:我的心里只有你 时间:2022-10-22 12:41:05
前言
由于android M的popupwindow与之前版本不一致,笔者找不到能够代码监听物理返回键的方式,故另寻方式实现筛选菜单。5.0及之前的版本可用popupwindow实现,详情请参考popupwindow用法。
本篇采用Dialog实现。
实现步骤
1、设置主题
一般设置如下
<style name="Translucent_NoTitle" parent="android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
<item name="android:background">#00000000</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:colorBackgroundCacheHint">@null</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:backgroundDimEnabled">false</item><span style="white-space:pre"> </span>背景暗淡效果
</style>
也可使用android.R.style.Theme_Panel和android.R.style.Theme_Light_Panel。android.R.style.Theme_Panel代码如下,其与上面是一样的。
<style name="Theme.Panel">
<item name="windowBackground">@color/transparent</item>
<item name="colorBackgroundCacheHint">@null</item>
<item name="windowFrame">@null</item>
<item name="windowContentOverlay">@null</item>
<item name="windowAnimationStyle">@null</item>
<item name="windowIsFloating">true</item>
<item name="backgroundDimEnabled">false</item>
<item name="windowIsTranslucent">true</item>
<item name="windowNoTitle">true</item>
</style>
2、设置内容的宽高
我们通过WindowManager.LayoutParams实现。
WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
layoutParams.width = screenWidth;
layoutParams.height = contentHeight;
layoutParams.gravity = Gravity.TOP;
layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; //不阻塞事件传递到后面的窗口
getWindow().setAttributes(layoutParams);
这里,设置layoutParams.flags |= WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL; 则后面窗口的按钮可响应触摸事件(例,HorizontalScrollView能横向滚动)。
3、设置动画
通过ValueAnimator实现。
enter = ValueAnimator.ofFloat(0, 1f).setDuration(350);
enter.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
dialogContent.setTranslationY((1 - animation.getAnimatedFraction()) * -contentHeight);
}
});
out = ValueAnimator.ofFloat(0, 1f).setDuration(350);
out.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
dialogContent.setTranslationY(animation.getAnimatedFraction() * -contentHeight);
}
});
out.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
dismiss();
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
上面enter和out进行一系列设置,对out动画加开始结束监听。enter的start()方法在onStart()中调用
@Override
protected void onStart() {
super.onStart();
dialogContent.post(new Runnable() {
@Override
public void run() {
enter.start();
}
});
}
通过view的post方式,enter.start()会在view hierarchy(view树)构建完后执行(即视图构建完后执行)。view.post源码:
public boolean post(Runnable action) {
final AttachInfo attachInfo = mAttachInfo;
if (attachInfo != null) {
return attachInfo.mHandler.post(action);
}
// Assume that post will succeed later
ViewRootImpl.getRunQueue().post(action);
return true;
}
第七行为关键代码,ViewRootImpl为视图层级的顶部,实现了view和WindowManager之间的必要协议。RunQueue:运行队列用来排入没有handler关联的view的以后工作。
所以这里dialog的视图显示时会调用enter.start()方法.
监听返回键:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
out.start();
return true;
}
return super.onKeyDown(keyCode, event);
}
out动画执行完后,onAnimationEnd中调用dismiss()方法。
4、在点击的view下显示出来
public void showAsDropView(View view) {
WindowManager.LayoutParams lp = getWindow().getAttributes();
lp.width = screenWidth;
int[] location = new int[2];
view.getLocationOnScreen(location);
// view.getLocationInWindow(location);<span style="white-space:pre"> </span>这里跟上面一句的效果一样,不知有什么区别
lp.y = location[1]-PhoneConstant.statusHeight+view.getHeight();
lp.gravity = Gravity.TOP;
getWindow().setAttributes(lp);
contentTop = location[1];
show();
}
PhoneConstant.statusHeight为状态栏的高度,其通过反射获取
//反射获取状态栏高度
Class<?> c = null;
Object obj = null;
Field field = null;
int x = 0, sbar = 0;
try {
c = Class.forName("com.android.internal.R$dimen");
obj = c.newInstance();
field = c.getField("status_bar_height");
x = Integer.parseInt(field.get(obj).toString());
PhoneConstant.statusHeight = getResources().getDimensionPixelSize(x);
} catch(Exception e1) {
e1.printStackTrace();
}
也可通过以下方式获取
Rect outRect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(outRect);
不过直接放在activity的onCreate中无效,只有界面绘制出来了才能获取到,可通过view.post()方式获取。
效果图:
另外,继承自AlertDialog的自定义dialog点击edittext不弹出软键盘,所以一般继承自Dialog。
控制对话框输入法的弹出,调用
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE|WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
来源:https://blog.csdn.net/y444400/article/details/50706623
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Java类型擦除机制
Logger.getLogger()与LogFactory.getLog()的区别详解
windows下jar包开机自动重启的步骤
C#下使用XmlDocument操作XML详解
java 如何实现正确的删除集合中的元素
![](https://img.aspxhome.com/file/2023/0/67680_0s.jpg)
SpringBoot整合Quartz实现定时任务详解
![](https://img.aspxhome.com/file/2023/4/67144_0s.png)
Java如何实现List自定义排序
Android实现蓝牙聊天功能
![](https://img.aspxhome.com/file/2023/7/122077_0s.png)
c#实现winform屏幕截图并保存的示例
在WCF数据访问中使用缓存提高Winform字段中文显示速度的方法
![](https://img.aspxhome.com/file/2023/5/125975_0s.png)
CCF考试试题之门禁系统java解题代码
Android 内存优化知识点梳理总结
![](https://img.aspxhome.com/file/2023/4/137634_0s.png)
java解析多层嵌套json字符串问题
![](https://img.aspxhome.com/file/2023/5/84315_0s.png)
C#实现输入10个数存入到数组中并求max和min及平均数的方法示例
Java C++ 算法题解leetcode652寻找重复子树
![](https://img.aspxhome.com/file/2023/0/61120_0s.jpg)
Android登录记住多个密码的实现方法
![](https://img.aspxhome.com/file/2023/9/139239_0s.png)
JDBC核心技术详解
![](https://img.aspxhome.com/file/2023/9/63169_0s.png)
实例讲解Android中的AutoCompleteTextView自动补全组件
![](https://img.aspxhome.com/file/2023/1/122121_0s.jpg)
详解androidstudio项目上传到github方法以及步骤
![](https://img.aspxhome.com/file/2023/0/139330_0s.png)
Java设计模式之模版方法模式简介
![](https://img.aspxhome.com/file/2023/0/61130_0s.jpg)