在Android中创建自定义对话框

14

我正在尝试在Android中创建一个自定义对话框,但是无论我尝试什么,都无法改变对话框的宽度。它始终保持不变。以下是我设置为对话框内容的视图。

<RelativeLayout  
android:id="@+id/current_stats"
android:layout_width="wrap_content"
android:layout_height="wrap_content" 
android:visibility="visible">
<ImageView android:id="@+id/player_image" 
    android:src="@drawable/person"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"/>
<TextView   
    android:id="@+id/player_name"
    android:layout_below="@id/player_image"
    android:layout_centerHorizontal="true"
    android:text="Raja Ram"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
<ImageButton android:id="@+id/dialog_close"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:background="#00000000"
    android:src="@drawable/close_button_selector"/>
<ImageButton android:id="@+id/dialog_flip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:background="#00000000"
    android:src="@drawable/rotate"/>
</RelativeLayout>

正如您所看到的,在这个代码示例中我到处使用wrap_content。此外,我尝试了以下选项:

1)在创建对话框时设置自定义样式,就像这样。

dialog = new Dialog(this,R.style.Theme_Dialog);

并且样式如下

<resources>
<style name="Theme" parent="android:Theme">
</style>
<style name="Theme.Dialog">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
    <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
</style>
</resources>

2)在onCreateDialog()中设置视图参数,例如:

LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.player_info, null, false);
ViewGroup.LayoutParams p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
dialog.setContentView(v,p);

3)我还尝试在onCreateDialog()方法中设置Window参数,如下所示:

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width=WindowManager.LayoutParams.WRAP_CONTENT;
dialog.getWindow().setAttributes(params);

但是我还是没有运气。有人能帮我解决这个问题吗?我做错了什么吗?另外,你能否建议我如何设置对话框窗口的x和y位置?

谢谢

5个回答

32
        dialog = new Dialog(this,android.R.style.Theme_Translucent_NoTitleBar);

        dialog.setContentView(R.layout.custom_dialog);

         LayoutParams lp=dialog.getWindow().getAttributes();    
         lp.x=100;lp.y=100;lp.width=100;lp.height=200;lp.gravity=Gravity.TOP | Gravity.LEFT;
         lp.dimAmount=0;            
         lp.flags=LayoutParams.FLAG_LAYOUT_NO_LIMITS | LayoutParams.FLAG_NOT_TOUCH_MODAL;
    //   dialog.getWindow().setAttributes(lp);

         dialog.show();

这对我非常有效...


稍微调整一下以适应我的需求后,工作得非常好。加一! - user577732
在这个问题上花费了许多小时后,您的解决方案很好地修复了它。谢谢。 - omega
1
有人能告诉我...这些x、y、宽度和高度是以px、dp还是sp格式表示的吗?谢谢! - Veljko
请使用 WindowManager.LayoutParams lp=.. 而不是 LayoutParams lp=..,否则 x、y 等参数会出错。 - Viswanath Lekshmanan

2
你的问题是它占据了整个屏幕的宽度,还是你无法控制它占据屏幕的大小?
如果要使你的活动作为对话框而不是占据整个屏幕,请在清单中将你的活动设置为对话框主题。
<activity android:theme="@android:style/Theme.Dialog">

对话框的大小将根据您的布局要求自适应。如果您想使其更宽,您需要让布局变宽。(调整图片大小、添加填充等)。
我不知道如何定位对话框窗口。。我认为它总是居中的,但我可能错了。

谢谢您的回复。我的问题是我想要它更像一个弹出窗口。而不是使用整个屏幕的宽度,我只想让它占用所需的宽度来容纳视图。并且我没有在该对话框中启动单独的活动。我有兴趣显示一个窗口,允许用户执行一些操作,并应允许一些动画。我甚至尝试了PopupWindow,但由于某种原因,我的自定义动画与弹出窗口不兼容。希望我已经清楚地表达了我的要求。您有什么建议吗? - rajaramyadhav
一个对话框主题的活动看起来像是一个弹出窗口。这个问题展示了它的一个例子:http://stackoverflow.com/questions/1967085/android-how-to-resize-dialog。 - Cheryl Simon

1
经过很长时间的研究,我终于找到了问题所在。问题出在我使用相对布局的方式上。我想我没有正确地指定相对位置。当我将其更改为线性布局时,它就正常工作了。它不会占用整个屏幕,而只是需要的部分。

0

我可以使用getWindow().setAttributes(params)来改变对话框的宽度、高度和位置。


-3
在您的自定义对话框类的onCreate()方法中执行以下操作。
@Override
    protected void onCreate(Bundle savedInstanceState) {
getWindow().setLayout(x, y);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接