如何创建将在水平维度上充满的对话框

2023-04-24移动开发问题
2

本文介绍了如何创建将在水平维度上充满的对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

当我在布局中使用时,它指定了这个对话框 android:layout_width="match_parent" 我得到了这个对话框:

When I use in layout, which specifies this dialog android:layout_width="match_parent" I get this dialog:

我需要更宽的对话框.有什么想法吗?

I need dialog which will be wider. Any ideas?

推荐答案

您可以通过抓取对话框使用的 Window 对象并重置宽度来实现.这是一个简单的例子:

You can do that by grabbing the Window object that the dialog uses, and resetting the width. Here's a simple example:

//show the dialog first
AlertDialog dialog = new AlertDialog.Builder(this)
        .setTitle("Test Dialog")
        .setMessage("This should expand to the full width")
        .show();
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);

这是最终结果.请注意,如果您需要微调某些内容(例如更改背景等),您可以对对话框进行更多样式设置.当我过去不得不这样做时,我通常使用这种方法,并在构建器类上使用 setView() 自定义对话框中使用的布局.

Here's the end result. Note that there's a lot more styling you can do to the dialog if you need to fine tune things (like change the background, etc). When I've had to do this in the past, I usually use this method, and customize the layout used in the dialog with setView() on the builder class.

这篇关于如何创建将在水平维度上充满的对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

从 Documents 目录存储和读取文件 iOS 5
Storing and reading files from Documents directory iOS 5(从 Documents 目录存储和读取文件 iOS 5)...
2024-08-12 移动开发问题
9

如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?
How can i use MYSQL database connection in iphone application useing cocos2d?(如何在使用 cocos2d 的 iphone 应用程序中使用 MYSQL 数据库连接?)...
2024-08-12 移动开发问题
5

在 cocos2d 中平滑拖动一个 Sprite - iPhone
Smoothly drag a Sprite in cocos2d - iPhone(在 cocos2d 中平滑拖动一个 Sprite - iPhone)...
2024-08-12 移动开发问题
10

CCScrollView 滚动和触摸事件永远不会触发
CCScrollView scroll and touch events never firing(CCScrollView 滚动和触摸事件永远不会触发)...
2024-08-12 移动开发问题
1

使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘
removing jagged edges of my ropes using antialiasing of OpenGLES(使用 OpenGLES 的抗锯齿去除绳索的锯齿状边缘)...
2024-08-12 移动开发问题
34

cocos2d 在场景之间移动
cocos2d Moving between scene(cocos2d 在场景之间移动)...
2024-08-12 移动开发问题
2