gpt4 book ai didi

android - 模态/对话框未在宽度上应用匹配父布局

转载 作者:行者123 更新时间:2023-11-29 21:35:43 29 4
gpt4 key购买 nike

我有一个对话框/模式,我需要沿其宽度匹配父级。但是,它一直在做 wrap_content。有什么想法可以纠正这个问题吗?

这是我的布局

<?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>

这是程序 fragment

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 = animalsView.getWindow()
.getAttributes();

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

最佳答案

我遇到了同样的问题,但找不到在 XML 中执行此操作的解决方案。但是在.java文件中有一个解决方案。

    WindowManager.LayoutParams params = animalsView.getWindow().getAttributes();
params.gravity = Gravity.BOTTOM;
params.height = 100;
params.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT;

关于android - 模态/对话框未在宽度上应用匹配父布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18475754/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com