美好的一天,如果这似乎与以前被问过的问题重复,请道歉。
我有和Android App,我正在显示一个Dialog片段。我遇到的问题是,当基本活动显示时,会忽略Dialog Fragment的宽度。这是我onCreateDialog
函数中的代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Dialog dialog = new Dialog(getActivity());
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
LayoutInflater layoutInflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout2 = layoutInflater.inflate(R.layout.fragment_item_dialog, null);
dialog.setContentView(layout2);
setStyle(DialogFragment.STYLE_NORMAL, R.style.MyDialog);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.setGravity(Gravity.TOP|Gravity.LEFT);
WindowManager.LayoutParams params = window.getAttributes();
params.x = 20;
params.y = 470;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.copyFrom(window.getAttributes());
window.setAttributes(params);
// -- more code here
}
这是我的xml文件fragment_item_dialog
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="wrap_content" >
<!--- code here ---!>
</RelativeLayout>
正确地遵循了高度,但Android仍然设置宽度以匹配父级,即使我告诉它要包装内容。我的对话框Fragment中的组件不超过400dp,我不知道为什么Android会强制我的布局与父级匹配。
有谁知道如何解决这个问题?很感谢任何形式的帮助。感谢。
答案 0 :(得分:2)
使它工作,在onStart()
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
if (getDialog() == null)
return;
int width = 1100;
int height = getResources().getDisplayMetrics().heightPixels;
getDialog().getWindow().setLayout(width,
WindowManager.LayoutParams.WRAP_CONTENT);
}