当前位置: 首页 > wzjs >正文

免费制作网站的软件网页淘宝

免费制作网站的软件,网页淘宝,广东省人防工程建设网站,市场营销数字营销实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…

实现弹窗随键盘上移的核心思路

在Android中,可以通过监听键盘的显示和隐藏事件,动态调整弹窗的位置。关键点在于获取键盘高度,并计算剩余屏幕空间以重新定位弹窗。

// 在Activity或Fragment中设置键盘监听
val rootView = findViewById<View>(android.R.id.content)
rootView.viewTreeObserver.addOnGlobalLayoutListener {val rect = Rect()rootView.getWindowVisibleDisplayFrame(rect)val screenHeight = rootView.rootView.heightval keyboardHeight = screenHeight - rect.bottomif (keyboardHeight > screenHeight * 0.15) {// 键盘显示,调整弹窗位置adjustDialogPosition(keyboardHeight)} else {// 键盘隐藏,恢复默认位置resetDialogPosition()}
}

创建自定义弹窗布局

使用Dialog或DialogFragment时,需要确保布局可以动态调整位置。示例布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/dialog_container"android:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:id="@+id/dialog_content"android:layout_width="300dp"android:layout_height="wrap_content"android:layout_gravity="center"android:orientation="vertical"android:padding="16dp"><!-- 弹窗内容 --></LinearLayout>
</FrameLayout>

动态调整弹窗位置代码

通过修改布局参数实现位置调整:

private fun adjustDialogPosition(keyboardHeight: Int) {val dialogContent = dialog.findViewById<View>(R.id.dialog_content)val params = dialogContent.layoutParams as FrameLayout.LayoutParamsval screenHeight = resources.displayMetrics.heightPixelsval targetY = (screenHeight - keyboardHeight) / 2 - dialogContent.height / 2params.topMargin = targetYdialogContent.layoutParams = params
}private fun resetDialogPosition() {val dialogContent = dialog.findViewById<View>(R.id.dialog_content)val params = dialogContent.layoutParams as FrameLayout.LayoutParamsparams.topMargin = 0params.gravity = Gravity.CENTERdialogContent.layoutParams = params
}

处理WindowSoftInputMode

在AndroidManifest.xml中为Activity设置正确的软键盘模式:

<activityandroid:name=".YourActivity"android:windowSoftInputMode="adjustResize|stateHidden" />

注意事项

  1. 键盘高度计算需要排除系统状态栏和导航栏的影响
  2. 在横屏模式下需要特殊处理布局逻辑
  3. 不同Android版本可能存在行为差异,需要充分测试
  4. 对于DialogFragment,需要在onCreateView中获取根视图进行监听

是的,这是我直接使用AI生成的文章,看了下,大致都实现了,感觉现在博客这条下坡路确实要走到底了啊。

下面是我的代码:

 @Overridepublic void onStart() {super.onStart();// 在对话框显示后设置键盘监听setupKeyboardListener();}/*** 设置键盘监听*/private void setupKeyboardListener() { // 设置全局布局监听,检测键盘状态变化if (mContext instanceof android.app.Activity) {android.app.Activity activity = (android.app.Activity) mContext;View rootView = activity.findViewById(android.R.id.content);if (rootView != null) {mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {checkKeyboardStatus();}};rootView.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);}}}/*** 检查键盘状态*/private void checkKeyboardStatus() {if (mContext instanceof android.app.Activity) {android.app.Activity activity = (android.app.Activity) mContext;View rootView = activity.findViewById(android.R.id.content);if (rootView != null) {android.graphics.Rect rect = new android.graphics.Rect();rootView.getWindowVisibleDisplayFrame(rect);int screenHeight = rootView.getHeight();int visibleHeight = rect.bottom - rect.top;// 判断键盘是否弹起(可视区域高度小于屏幕高度的75%)boolean keyboardVisible = visibleHeight < screenHeight * 0.75;if (keyboardVisible && !isKeyboardShown) {// 键盘弹起,计算对话框在剩余可见区域的居中位置isKeyboardShown = true;adjustDialogPosition(true, visibleHeight);} else if (!keyboardVisible && isKeyboardShown) {// 键盘收起isKeyboardShown = false;adjustDialogPosition(false, screenHeight);}}}}/*** 调整对话框位置* @param keyboardShown 键盘是否显示* @param visibleHeight 可见区域高度*/private void adjustDialogPosition(boolean keyboardShown, int visibleHeight) {if (getDialogHelper() == null) {return;}try {View contentView = getDialogHelper().getContentView();if (keyboardShown) {// 键盘弹起时,让对话框在剩余可见区域中居中显示getDialogHelper().setGravity(Gravity.CENTER_HORIZONTAL | Gravity.TOP);// 对话框高度(根据布局文件dialog_quick_greet.xml约246dp)int dialogHeight = (int) (246 * mContext.getResources().getDisplayMetrics().density);// 计算让对话框在可见区域居中的上边距// 公式:(可见区域高度 - 对话框高度) / 2int centeredTopMargin = (visibleHeight - dialogHeight) / 2;// 设置最小边距,避免对话框贴着屏幕顶部int minTopMargin = (int) (50 * mContext.getResources().getDisplayMetrics().density);int topMargin = Math.max(centeredTopMargin, minTopMargin);setContentViewMargin(contentView, topMargin);} else {// 键盘收起时,恢复默认的居中位置 自己的方法getDialogHelper().setGravity(Gravity.CENTER);setContentViewMargin(contentView, 0);}} catch (Exception e) {e.printStackTrace();}}/*** 设置内容视图的上边距*/private void setContentViewMargin(View contentView, int topMargin) {if (contentView != null && contentView.getLayoutParams() instanceof android.view.ViewGroup.MarginLayoutParams) {android.view.ViewGroup.MarginLayoutParams params = (android.view.ViewGroup.MarginLayoutParams) contentView.getLayoutParams();params.topMargin = topMargin;contentView.setLayoutParams(params);}}

http://www.dtcms.com/wzjs/571056.html

相关文章:

  • html5响应式网站制作tk免费域名注册
  • 如何开一家网站建设公司厦门网站备案
  • 网站 软件网站报价系统
  • 用.net做购物网站推广网站文案素材
  • wordpress 改变滑页连云港seo公司
  • 怎么让网站排名上去天津网站开发网站
  • 怎么给自己公司做网站wordpress写文章500
  • 石家庄自适应网站建设常德市建设局网站
  • 企业网站色彩asp做登入网站
  • 潇朋友免费班级网站建设系统天津企业网站制作公司
  • 专门做spa的网站百度建设公司网站
  • 手机门户网站开发网站备案如何申请
  • 建设网站网址是多少中小微企业名录查询系统
  • 广西建设工会网站oa系统登录入口
  • 江苏省医院网站建设管理规范网站文本编辑器
  • 网站关键词排名优化系统岳阳建设银行网站
  • 太原建网站的公司wordpress下载5.11
  • 如何用c 做网站背景住房和城乡建设部网站第九批
  • logo网站做内网网站教程
  • 鄂尔多斯市网站建设闵行网络推广公司
  • 甜点网站要怎么做重庆做网站找谁
  • 在线h5免费制作网站查询网址域名ip地址
  • 营销型网站建设ppt陕西网站推广费用
  • 2018年主流网站开发语言网络直接营销渠道
  • 企业微网站哪家好为wordpress配置邮箱服务
  • 旅游分析 网站品牌logo查询网
  • 网站开发制作包括哪些的基本流程手机网站改版公司
  • phpcms 怎么做视频网站首页在网站做的pdf有水印如何删除
  • 定期做图书推荐的网站十大互联网营销公司
  • 网站制作公司上海做互联网推广的公司