Popup(使用PopupWindow或Dialog)填充屏幕的宽度,没有明显的原因

编程入门 行业动态 更新时间:2024-10-28 04:17:22
本文介绍了Popup(使用PopupWindow或Dialog)填充屏幕的宽度,没有明显的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我试图让一个弹出窗口行为! =)

问题是弹出窗口填充整个屏幕的宽度,即使布局清楚地表示它应该是wrap_content。如果我使用Dialog或PopupWindow,这并不重要。

首先,弹出窗口的XML布局,popup_message.xml:

<?xml version =1.0encoding =utf-8?> < LinearLayout xmlns:android =schemas.android/apk/res/android android:layout_width =wrap_content android:layout_height =match_parent android:layout_margin =20dp android:background =@ android:color / transparent android:orientation =vertical> < LinearLayout android:layout_width =match_parent android:layout_height =40dp android:orientation =horizo​​ntal android: layout_gravity =center> < TextView android:layout_width =match_parent android:layout_height =fill_parent android:layout_gravity =center android: background =@ android:color / transparent android:gravity =center_vertical | center_horizo​​ntal android:orientation =horizo​​ntal android:paddingLeft =10dp android :text =@ string / new_message android:textColor =#000000 android:textAllCaps =true> < / TextView> < / LinearLayout> < RelativeLayout android:layout_width =wrap_content android:layout_height =wrap_content android:padding =15dp android: background =#ffffff android:gravity =center_horizo​​ntal android:orientation =vertical> < TextView android:id =@ + id / popup_message_textView android:layout_width =wrap_content android:layout_height =wrap_content android:textAppearance =?android:attr / textAppearanceMedium android:textCursorDrawable =@ null> < / TextView> < Button android:id =@ + id / jobview_pickup_start_submit android:layout_width =fill_parent android:layout_height =35dp android:layout_below =@ + id / popup_message_textView android:layout_gravity =center_vertical | center_horizo​​ntal android:layout_marginBottom =15dp android:layout_marginTop =15dp android:gravity =center_horizo​​ntal android:text =Confirm android:textColor =#000000/> < / RelativeLayout> < / LinearLayout>

我正在使用的代码:

LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); 查看输入= inflater.inflate(R.layout.popup_message,null); PopupWindow pw = new PopupWindow(input,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true); 查看v = findViewById(R.id.main_deviceInfoBar); pw.showAtLocation(v,Gravity.CENTER,0,0);

我也尝试过:

对话框dialogManualStart = new Dialog(MainActivity.this); dialogManualStart.getWindow()。setBackgroundDrawableResource(R.color.transparent); dialogManualStart.requestWindowFeature(MainActivity.this.getWindow()。FEATURE_NO_TITLE); dialogManualStart.setContentView(input); dialogManualStart.show();

总是,无论我使用什么代码,看起来像这样:

您可以看到,它总是填补屏幕的宽度。

问题:有谁可以告诉我为什么?

========编辑1 =============

我更改了按钮到wrap_content(根据Boogers的建议),然后看起来像这样:

这是非常奇怪的行为,而不是我的预期。

如果我将其更改为match_parent,则返回full width,即从左到右扩展。

解决方案

这是您的问题:

< Button android:id =@ + id / jobview_pickup_start_submit更改为fill_parent到wrap_content或match_parent

I am trying to get a popup to behave! =)

The problem is that the popup "fills the width" of the entire screen, even though the layout clearly says that it should "wrap_content". It doesnt matter if I use Dialog or PopupWindow.

First, the XML layout of the popup, popup_message.xml:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="schemas.android/apk/res/android" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_margin="20dp" android:background="@android:color/transparent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="40dp" android:orientation="horizontal" android:layout_gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center" android:background="@android:color/transparent" android:gravity="center_vertical|center_horizontal" android:orientation="horizontal" android:paddingLeft="10dp" android:text="@string/new_message" android:textColor="#000000" android:textAllCaps="true" > </TextView> </LinearLayout> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="15dp" android:background="#ffffff" android:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/popup_message_textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:textCursorDrawable="@null" > </TextView> <Button android:id="@+id/jobview_pickup_start_submit" android:layout_width="fill_parent" android:layout_height="35dp" android:layout_below="@+id/popup_message_textView" android:layout_gravity="center_vertical|center_horizontal" android:layout_marginBottom="15dp" android:layout_marginTop="15dp" android:gravity="center_horizontal" android:text="Confirm" android:textColor="#000000" /> </RelativeLayout> </LinearLayout>

And the code I am using:

LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE); View input = inflater.inflate(R.layout.popup_message, null); PopupWindow pw = new PopupWindow(input, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, true); View v = findViewById(R.id.main_deviceInfoBar); pw.showAtLocation(v, Gravity.CENTER, 0, 0);

I have also tried this:

Dialog dialogManualStart = new Dialog(MainActivity.this); dialogManualStart.getWindow().setBackgroundDrawableResource(R.color.transparent); dialogManualStart.requestWindowFeature(MainActivity.this.getWindow().FEATURE_NO_TITLE); dialogManualStart.setContentView(input); dialogManualStart.show();

And it always, no matter what code I use, looks like this:

As you can see, its always filling the width of the screen.

Question: Can anyone tell me why?

======== EDIT 1 =============

I changed the Button to "wrap_content" (according to Boogers suggestion), and then it looks like this:

This is very strange behaviour, not what I expected.

If I change it to "match_parent", its back to "full width", ie it extends all the way from left to right.

解决方案

This is your problem:

<Button android:id="@+id/jobview_pickup_start_submit" android:layout_width="fill_parent" ...

Change that from "fill_parent" to "wrap_content" or "match_parent"

更多推荐

Popup(使用PopupWindow或Dialog)填充屏幕的宽度,没有明显的原因

本文发布于:2023-11-24 01:22:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623462.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   屏幕   原因   Popup   PopupWindow

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!