Android 模式/对话框未在宽度上应用匹配父布局
我有一个对话框/模式,我需要沿其宽度匹配父对象。然而,它一直在做包装内容。有没有办法纠正这个问题 这是我的布局Android 模式/对话框未在宽度上应用匹配父布局,android,android-layout,modal-dialog,android-linearlayout,android-dialog,Android,Android Layout,Modal Dialog,Android Linearlayout,Android Dialog,我有一个对话框/模式,我需要沿其宽度匹配父对象。然而,它一直在做包装内容。有没有办法纠正这个问题 这是我的布局 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/my_root" android:layout_width="match_parent"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/my_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#55555555" >
<Button
android:id="@+id/dogs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onDogsClicked"
android:text="Dogs" />
<Button
android:id="@+id/cats"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="Cats"
android:text="Cat" />
<Button
android:id="@+id/snake"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#FF0000"
android:clickable="true"
android:gravity="center"
android:onClick="onSnakeClicked"
android:text="Snake" />
<Button
android:id="@+id/sheep"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#AAAAAA"
android:clickable="true"
android:gravity="center"
android:onClick="onSheepClicked"
android:text="Sheep" />
</LinearLayout>
</LinearLayout>
我也有同样的问题,我找不到在XML中实现它的解决方案。但是在.java文件中有一个解决方案
WindowManager.LayoutParams params = animalsView.getWindow().getAttributes();
params.gravity = Gravity.BOTTOM;
params.height = 100;
params.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
试试这个
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
........
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
......
animalsView.show();
}
我想你应该试试这个。当我面对这个问题时,我正在使用覆盖层。希望这对你有帮助
Dialog animalsView;
public void onAnimalsClicked(View v) {
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.animals_layout,
(ViewGroup) findViewById(R.id.my_root));
animalsView = new Dialog(this, R.style.CustomDialog);
WindowManager.LayoutParams wmlp = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSPARENT);
wmlp.gravity = Gravity.BOTTOM;
wmlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
animalsView.getWindow().setAttributes(wmlp);
animalsView.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
animalsView.setContentView(dialoglayout);
animalsView.getWindow().setBackgroundDrawable(new ColorDrawable(0));
animalsView.show();
}
得到了类似的问题,并通过了大量关于此问题的StackOverflow帖子 我尝试了所有可能的解决方案(主要是以所有可能的方式更改对话框窗口的布局配置),但没有一个有效 然而,当比较
match_parent
工作的对话框的XML布局和它不工作的对话框的XML布局时,我最终发现了一种奇怪的方法使其工作:
我必须将对话框的layout\u height
设置为match\u parent
,如下所示:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
</LinearLayout>
我现在也面临同样的问题。你能找到解决办法吗?
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="@dimen/activity_horizontal_margin"
android:layout_width="match_parent"
android:layout_height="match_parent">
// ...
</LinearLayout>
final Dialog dialogLayout = new Dialog(mContext);
dialogLayout.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogLayout.setCancelable(false);
View layout = mContext.getLayoutInflater().inflate(R.layout.popup_dialog_introduce_myself, null);
dialogLayout.setContentView(layout);
// ...
dialogLayout.show();