How to customize the width and height when show an Activity as a Dialog(将活动显示为对话框时如何自定义宽度和高度)
问题描述
如果我定义了一个活动:
If I have defined a Activity:
public class DialogActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(dialog_activity.xml);
}
}
我想像对话框一样显示上面的活动,所以在 AndroidManifest.xml 文件中,我像下面这样声明这个活动:
I would like to display the above activity like a dialog, so in the AndroidManifest.xml file, I declare this activity like below:
<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"/>
此时一切正常,我的 DialogActivity
显示为对话框.
Everything is fine at this point, my DialogActivity
showed as a dialog.
问题是如何自定义DialogActivity
的宽高,让它更像一个小对话框?(目前默认占据大部分屏幕)
The problem is How to customize the width and height of the DialogActivity
to make it more like a small dialog? (Currently it occupies most of the screen by default)
----------------更新-----------------------
我定义了一个自定义主题,如下所示:
I defined a custom theme like below:
<style name="myDialog" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:width">100dip</item>
<item name="android:height">100dip</item>
</style>
然后,将 AndroidManifest.xml 中的 DialogActivity
声明为.
<activity android:name=".DialogActivity" android:theme="@style/myDialog"/>
我的 DialogActivity
现在甚至占据了整个屏幕:( . width 和 height 定义:
My DialogActivity
now even occupies the whole screen:( . The width and height definition:
<item name="android:width">100dip</item>
在主题中没有任何效果,为什么?
in the theme do not take any effect, why?
推荐答案
我终于找到了一种自定义 DialogActivity
大小的方法.
I finally figured out one way to customize the size of my DialogActivity
.
在DialogActivity
的onCreate()
方法里面,添加如下代码:
That's inside the onCreate()
method of DialogActivity
, add the following code:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -20;
params.height = 100;
params.width = 550;
params.y = -10;
this.getWindow().setAttributes(params);
这篇关于将活动显示为对话框时如何自定义宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将活动显示为对话框时如何自定义宽度和高度


基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01