android– 调整DialogFragment的宽度和高度

android– 调整DialogFragment的宽度和高度,第1张

概述我对DialogFragmnt的宽度高度有问题.这是我的代表DialogFragmetn的类:publicclassRecipeAddDialogFragmentextendsDialogFragment{privateArrayList<RecipeDialogItem>recipeDialogItems;privateRecipeAddDialogAdapterrecipeDialogAdapter;privateSt

我对DialogFragmnt的宽度和高度有问题.这是我的代表DialogFragmetn的类:

public class RecipeAddDialogFragment extends DialogFragment {    private ArrayList<RecipeDialogItem> recipeDialogItems;    private RecipeAddDialogAdapter recipeDialogAdapter;    private String recipeUniqueID;    private CoordinatorLayout coordinatorLayout;    private RecipeAddDialogFragment recipeDialogFragment;    @OverrIDe    public voID onStart() {        super.onStart();        if (getDialog() == null) {            return;        }        int dialogWIDth = 600;        int dialogHeight = 300;        getDialog().getwindow().setLayout(dialogWIDth, dialogHeight);        getDialog().setTitle(getString(R.string.recipe_dialog_Title));    }    @OverrIDe    public voID onCreate(Bundle bundle) {        super.onCreate(bundle);        setStyle(DialogFragment.STYLE_norMAL, R.style.Apptheme_DialogFragment);    }    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,                             Bundle savedInstanceState) {        return inflater.inflate(R.layout.dialog_fragment, container, false);    }    @OverrIDe    public voID onVIEwCreated(VIEw vIEw, Bundle savedInstanceState) {        recipeDialogItems = new ArrayList<>();        RecyclerVIEw dialogRecyclerVIEw = (RecyclerVIEw) vIEw.findVIEwByID(                R.ID.dialog_recycler_vIEw);        recipeDialogAdapter = new RecipeAddDialogAdapter(getContext(), recipeDialogItems,                R.layout.recipe_dialog_item);        recipeDialogAdapter.setRuIDClRdf(recipeUniqueID, coordinatorLayout, recipeDialogFragment);        dialogRecyclerVIEw.setHasFixedSize(true);        dialogRecyclerVIEw.setAdapter(recipeDialogAdapter);        dialogRecyclerVIEw.setLayoutManager(new linearlayoutmanager(getContext()));        fillRecipeDialogArray();    }    private voID fillRecipeDialogArray() {        String name = getString(R.string.add_to_favourites);        int icon = R.drawable.ic_heart_48dp;;        RecipeDialogItem dialogItem = new RecipeDialogItem();        dialogItem.setRowIcon(icon);        dialogItem.setRowOption(name);        recipeDialogItems.add(dialogItem);        recipeDialogAdapter.notifyDataSetChanged();    }    public voID setReferences(String recipeUniqueID, CoordinatorLayout coordinatorLayout,                              RecipeAddDialogFragment recipeDialogFragment) {        this.recipeUniqueID = recipeUniqueID;        this.coordinatorLayout = coordinatorLayout;        this.recipeDialogFragment = recipeDialogFragment;    }}

这是我在此DialogFragment中渗透的.xml:

<?xml version="1.0" enCoding="utf-8"?><linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    androID:gravity="center|left"    androID:padding="16dp"    androID:orIEntation="horizontal"    androID:clickable="true"    androID:background="?androID:attr/selectableItemBackground">    <!-- Option Icon -->    <ImageVIEw        androID:ID="@+ID/recipe_dialog_option_icon"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_marginRight="4dp"        androID:tint="@color/primary" />    <!-- Text Option -->    <TextVIEw        androID:ID="@+ID/recipe_dialog_option_text"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:textSize="18sp"        androID:textcolor="@color/primary_text" /></linearLayout>

问题是当我将它的大小设置为600 x 300时,它在我的1280×720设备中显示正常,但是例如当我的朋友在1920×1080分辨率下显示它时,对话框被包装并且仅显示标题.列表被包装,并未完整显示.我知道如何自动设置它的大小以适应每个显示器并显示整个对话框,它包含在内容中?

编辑

我已经想出将DialogFragment的宽度调整为它的内容,如下所示:

getDialog().getwindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,            WindowManager.LayoutParams.WRAP_CONTENT);    getDialog().setTitle(getString(R.string.recipe_dialog_Title));

但是高度不能正常工作:/

解决方法:

在DialogFragment.onResume()中:

int wIDth = getResources().getDimensionPixelSize(R.dimen.popup_wIDth);int height = getResources().getDimensionPixelSize(R.dimen.popup_height);        getDialog().getwindow().setLayout(wIDth, height);

在对话框的布局中:

androID:layout_wIDth="match_parent"androID:layout_height="match_parent"

可以采取整个屏幕:

getDialog().getwindow().setLayout(    getResources().getdisplayMetrics().wIDthPixels,    getResources().getdisplayMetrics().heightPixels);

希望有所帮助

在这里找到了这个想法How to set DialogFragment’s width and height?

总结

以上是内存溢出为你收集整理的android – 调整DialogFragment的宽度和高度全部内容,希望文章能够帮你解决android – 调整DialogFragment的宽度和高度所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1119419.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存