android 不同分辨率 popupwindow大小,关于popupwindow:Android弹出窗口无法填满屏幕大小吗?...

我正在尝试制作一个简单的弹出窗口。 但是每次我做一个,它最终都会变得很小……而不是我想要的长度。 这是弹出窗口的外观:

dd5b929084e5d15e554d66252eda6fd1.png

这是弹出窗口的布局:

android:id="@+id/popup_element"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#444444"

android:padding="10px"

android:orientation="vertical">

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:text="Transfering data"

android:textColor="@color/white"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:text="Status"

android:textColor="@color/white"/>

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Awaiting answer..."

android:paddingLeft="10sp"

android:textColor="@color/white"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="horizontal"

android:gravity="center_horizontal|bottom">

android:layout_width="100dp"

android:layout_height="100dp"

android:drawablePadding="3sp"

android:layout_centerHorizontal="true"

android:text="Cancel" />

这是我的Java代码:

private PopupWindow pw;

private void bindActivity() {

fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);

fabButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

initiatePopupWindow();

}

});

}

private void initiatePopupWindow() {

try {

//We need to get the instance of the LayoutInflater, use the context of this activity

LayoutInflater inflater = (LayoutInflater) ProfileView.this

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//Inflate the view from a predefined XML layout

View layout = inflater.inflate(R.layout.popup,

(ViewGroup) findViewById(R.id.popup_element));

// create a 300px width and 470px height PopupWindow

pw = new PopupWindow(layout, 300, 470, true);

// display the popup in the center

pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);

Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);

cancelButton.setOnClickListener(cancel_button_click_listener);

} catch (Exception e) {

e.printStackTrace();

}

}

private View.OnClickListener cancel_button_click_listener = new View.OnClickListener() {

public void onClick(View v) {

pw.dismiss();

}

};

无法弄清楚为什么它不起作用...

我如何获得想要的尺寸?

这是示例弹出窗口。stackoverflow.com/questions/13341560/

@TaiNguyen但是我不想使用Dialog Fragment,我想使用Popupwindow类。 我只想确保可以使用Popupwindow类创建它。

这是使用PopupWindow mrbool.com/how-to-implement-popup-window-in-android/28285的很好的示例。 您应该在showAtLocation之前设置setContentView。popupMessage.setContentView(layoutOfPopup);

create a 300px width and 470px height PopupWindow我在这里看不到问题,您得到的正是您要的;-)

请参阅如何制作一个简单的Android弹出窗口。 (这个答案曾经在这里,但是我将其移开,因为它更适合其他问题。)

在这里,您不能使用弹出窗口xml中的布局。 您必须使用主布局中的任何视图。 现在,我正在使用FloatingButton作为showAtLocation的视图。

fabButton = (ImageButton) findViewById(R.id.activity_profileView_FAB);

fabButton.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(final View v) {

initiatePopupWindow(v);

}

});

private void initiatePopupWindow(View v) {

try {

//We need to get the instance of the LayoutInflater, use the context of this activity

LayoutInflater inflater = (LayoutInflater) ProfileView.this

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

//Inflate the view from a predefined XML layout

View layout = inflater.inflate(R.layout.popup,

(ViewGroup) findViewById(R.id.popup_element));

// create a 300px width and 470px height PopupWindow

pw = new PopupWindow(layout, 300, 470, true);

// display the popup in the center

pw.showAtLocation(v, Gravity.CENTER, 0, 0);

TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);

Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);

cancelButton.setOnClickListener(cancel_button_click_listener);

} catch (Exception e) {

e.printStackTrace();

}

}

只是改变

pw = new PopupWindow(layout, 300, 470, true);

喜欢这个

pw = new PopupWindow(layout, LinearLayout.LayoutParams.MATCH_PARENT,

LinearLayout.LayoutParams.MATCH_PARENT, true);

如果您需要任何边距,请在弹出xml文件中执行。 同样在xml中使用android:layout_height =" wrap_content"。 这样做的优点是-在任何设备屏幕上,它看起来都是一样的(如果放了边距)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值