If I have defined a Activity:
如果我已经定义了一个Activity:
public class DialogActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(dialog_activity.xml);
}
}
I would like to display the above activity like a dialog, so in the AndroidManifest.xml file, I declare this activity like below:
我想像对话框一样显示上面的活动,所以在AndroidManifest.xml文件中,我声明这个活动如下:
<activity android:name=".DialogActivity" android:theme="@android:style/Theme.Dialog"/>
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)
问题是如何自定义DialogActivity的宽度和高度,使其更像一个小对话框? (目前它默认占据大部分屏幕)
----------------------------Update-----------------------
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>
Then, declare the DialogActivity
in AndroidManifest.xml as.
然后,将AndroidManifest.xml中的DialogActivity声明为。
<activity android:name=".DialogActivity" android:theme="@style/myDialog"/>
My DialogActivity
now even occupies the whole screen:( . The width and height definition:
我的DialogActivity现在甚至占据整个屏幕:(。宽度和高度定义:
<item name="android:width">100dip</item>
in the theme do not take any effect, why?
在主题上没有任何效果,为什么?
5 个解决方案
#1
41
I finally figured out one way to customize the size of my DialogActivity
.
我终于想出了一种自定义DialogActivity大小的方法。
That's inside the onCreate()
method of DialogActivity
, add the following code:
这是在DialogActivity的onCreate()方法内,添加以下代码:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -20;
params.height = 100;
params.width = 550;
params.y = -10;
this.getWindow().setAttributes(params);
#2
1
I had similar problem, I used following theme for my activity
我有类似的问题,我使用以下主题进行我的活动
android:theme="@style/Base.Theme.AppCompat.Light.Dialog"
The following code fixed my problem
以下代码修复了我的问题
setContentView(R.layout.activity_layout);
getWindow().setLayout(width(pixels),height(pixels));
#3
0
You need to supply your own dialog theme, see http://developer.android.com/guide/topics/ui/themes.html and How to change theme for AlertDialog
您需要提供自己的对话框主题,请参阅http://developer.android.com/guide/topics/ui/themes.html以及如何更改AlertDialog的主题
#4
0
You can set the layout height and width in layout xml file
您可以在布局xml文件中设置布局高度和宽度
You can even mention the values like "240px" for android:layout_width/android:layout_height
你甚至可以为android提供像“240px”这样的值:layout_width / android:layout_height
#5
0
There's a one-line code without hassling with the LayoutParams that is
有一个单行代码,没有对LayoutParams的麻烦
getWindow().setLayout((int)(width*.8),(int)(height*.25));
This is what I've used before in one of my projects and now searching for, but found it in the same project.
这是我之前在我的一个项目中使用过的,现在正在搜索,但在同一个项目中找到它。
#1
41
I finally figured out one way to customize the size of my DialogActivity
.
我终于想出了一种自定义DialogActivity大小的方法。
That's inside the onCreate()
method of DialogActivity
, add the following code:
这是在DialogActivity的onCreate()方法内,添加以下代码:
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = -20;
params.height = 100;
params.width = 550;
params.y = -10;
this.getWindow().setAttributes(params);
#2
1
I had similar problem, I used following theme for my activity
我有类似的问题,我使用以下主题进行我的活动
android:theme="@style/Base.Theme.AppCompat.Light.Dialog"
The following code fixed my problem
以下代码修复了我的问题
setContentView(R.layout.activity_layout);
getWindow().setLayout(width(pixels),height(pixels));
#3
0
You need to supply your own dialog theme, see http://developer.android.com/guide/topics/ui/themes.html and How to change theme for AlertDialog
您需要提供自己的对话框主题,请参阅http://developer.android.com/guide/topics/ui/themes.html以及如何更改AlertDialog的主题
#4
0
You can set the layout height and width in layout xml file
您可以在布局xml文件中设置布局高度和宽度
You can even mention the values like "240px" for android:layout_width/android:layout_height
你甚至可以为android提供像“240px”这样的值:layout_width / android:layout_height
#5
0
There's a one-line code without hassling with the LayoutParams that is
有一个单行代码,没有对LayoutParams的麻烦
getWindow().setLayout((int)(width*.8),(int)(height*.25));
This is what I've used before in one of my projects and now searching for, but found it in the same project.
这是我之前在我的一个项目中使用过的,现在正在搜索,但在同一个项目中找到它。