在 Android 中创建自定义对话框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3399667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:01:20  来源:igfitidea点击:

Creating a custom dialog in Android

androiddialog

提问by rajaramyadhav

I am trying to create a custom dialog in Android. But whatever I tried to do, I am not able to change the width of the dialog. It just remains the same. The following is the view that I am setting as the content for the dialog.

我正在尝试在 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>

As you can see, everywhere I am using wrap_content in this code sample. Also I tried the following options

如您所见,在此代码示例中,我到处都在使用 wrap_content。我也尝试了以下选项

1) Setting a custom style while creating the Dialog like this.

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

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

And the style as follows

样式如下

<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) Setting the parameters for the view in the onCreateDialog() like this

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) I also tried to set the Window parameters like this in the onCreateDialog() method

3)我也尝试在 onCreateDialog() 方法中设置像这样的 Window 参数

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

But again no luck. Can someone help me out with this issue. Am I doing something wrong?? Also can you please suggest me how to set the x and y postions for the Dialog window?

但再次没有运气。有人可以帮我解决这个问题。难道我做错了什么??另外,您能否建议我如何设置对话框窗口的 x 和 y 位置?

Thanks

谢谢

采纳答案by rajaramyadhav

I figured out the problem after a long time. The problem was with the way I used Relative layout. I guess I have not specified the relative position properly. When I changed that to linear layout it worked fine. It was not using up the whole screen but only whatever is required.

很长一段时间后我发现了这个问题。问题在于我使用相对布局的方式。我想我没有正确指定相对位置。当我将其更改为线性布局时,它工作正常。它没有用完整个屏幕,而只是用光了所需的一切。

回答by Jithin

        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();

This worked well for me....

这对我来说效果很好......

回答by Cheryl Simon

Is your problem that it takes the full width of the screen, or that you can't control how much of the screen is it taking up?

你的问题是它占据了屏幕的整个宽度,还是你无法控制它占据了多少屏幕?

To make your activity act as a dialog, and not take up the full with you should set your activity to have the dialog theme in the manifest:

要使您的活动充当对话框,而不是全部使用,您应该将活动设置为在清单中具有对话框主题:

<activity android:theme="@android:style/Theme.Dialog">

The dialog will be as large as is required by your layout. If you want it to be wider, you need to make your layout wider. (Adjust size of images, add padding, etc).

该对话框将与您的布局所需的一样大。如果你想让它更宽,你需要让你的布局更宽。(调整图像大小,添加填充等)。

I'm not aware of a way to position the dialog window.. I think it is always centered, I could be wrong though.

我不知道定位对话窗口的方法..我认为它总是居中,但我可能是错的。

回答by Jithin

I could change the width,height and the position of the Dialog using getWindow().setAttributes(params)

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

回答by shontauro

in the onCreate()method of yout custom dialog class do the next

在你onCreate()自定义对话框类的方法中做下一个

@Override
    protected void onCreate(Bundle savedInstanceState) {
getWindow().setLayout(x, y);
}