android– 如何设置FragmentDialog大小以包装到ListView

android– 如何设置FragmentDialog大小以包装到ListView,第1张

概述我有RecyclerView,当我长时间点击其中一个项目FragmentDialog时出现自定义布局:|@text|.问题是FragmentDialog不会将区域仅包含在那两个元素中,而是占据几乎整个屏幕的位置.即使我设置布局以包装内容,问题仍然出现.这是问题所在:这是RecyclerViewcustom_itemXML:<?xmlversi

我有RecyclerVIEw,当我长时间点击其中一个项目FragmentDialog时出现自定义布局:| @ text |.问题是Fragment Dialog不会将区域仅包含在那两个元素中,而是占据几乎整个屏幕的位置.即使我设置布局以包装内容,问题仍然出现.这是问题所在:


这是RecyclerVIEw custom_item 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="wrap_content"    androID:g@R_404_6610@ty="center|left"    androID:padding="8dp"    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:tint="@color/primary"        androID:layout_marginRight="4dp"        androID:src="@drawable/ic_email_48dp" />    <!-- 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"        androID:text="Dodaj do ulubionych" /></linearLayout>

这是FragmentDialog布局:

<?xml version="1.0" enCoding="utf-8"?><linearLayout    xmlns:androID="http://schemas.androID.com/apk/res/androID"    androID:orIEntation="vertical"    androID:layout_wIDth="wrap_content"    androID:layout_height="wrap_content">    <androID.support.v7.Widget.RecyclerVIEw        androID:ID="@+ID/dialog_recycler_vIEw"        androID:layout_wIDth="match_parent"        androID:layout_height="match_parent" /></linearLayout>

和.class以防万一:

package com.example.nazwamarki.myapplication.recipedialog;import androID.os.Bundle;import androID.support.v4.app.DialogFragment;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.vIEw.Window;import com.example.nazwamarki.myapplication.R;import java.util.ArrayList;public class RecipeDialogFragment extends DialogFragment {    private ArrayList<RecipeDialogItem> recipeDialogItems;    private RecipeDialogAdapter recipeDialogAdapter;    @OverrIDe    public VIEw onCreateVIEw(LayoutInflater inflater, VIEwGroup container,                             Bundle savedInstanceState) {        getDialog().getwindow().requestFeature(Window.FEATURE_NO_Title);        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);        fillRecipeDialogArray(recipeDialogItems);        recipeDialogAdapter = new RecipeDialogAdapter(getContext(), recipeDialogItems,                R.layout.recipe_dialog_item);        dialogRecyclerVIEw.setHasFixedSize(true);        dialogRecyclerVIEw.setAdapter(recipeDialogAdapter);        dialogRecyclerVIEw.setLayoutManager(new linearlayoutmanager(getContext()));    }    private voID fillRecipeDialogArray(ArrayList<RecipeDialogItem> recipeDialogitem) {        RecipeDialogItem dialogItem;        String[] names = new String[] {"Account", "Star"};        int[] icons = new int[] {R.drawable.ic_account_plus_48dp, R.drawable.ic_account_star_48dp};        for (int i = 0; i < 2; i++) {            dialogItem = new RecipeDialogItem();            dialogItem.setRowIcon(icons[i]);            dialogItem.setRowOption(names[i]);            recipeDialogitem.add(dialogItem);        }    }}

解决方法:

将ListVIEw宽度传递给DialogFragment,然后在DialogFragment的onStart()中执行以下 *** 作

@OverrIDepublic voID onStart(){  super.onStart();  // safety check  if (getDialog() == null)    return;  int dialogWIDth = getArguments().getInt("wIDth"); // specify a value here  int dialogHeight = ... // specify a value here  getDialog().getwindow().setLayout(dialogWIDth, dialogHeight);  // ... other stuff you want to do in your onStart() method}

Showing DialogFragment的最佳实践

private voID showDialog() {    // DialogFragment.show() will take care of adding the fragment    // in a transaction.  We also want to remove any currently showing    // dialog, so make our own transaction and take care of that here.    FragmentManager fm = getSupportFragmentManager();    FragmentTransaction transaction = fm.beginTransaction();    Fragment prev = getSupportFragmentManager().findFragmentByTag("dialog");    if (prev != null) {        transaction.remove(prev);    }    transaction.addToBackStack(null);    // Create and show the dialog.    DialogFragment newFragment = RecipeDialogFragment.newInstance(wIDth);    newFragment.show(transaction, "dialog");}

RecipeDialogFragment

    /**--Static factory method---     * Create a new instance of RecipeDialogFragment, provIDing "wIDth"     * as arguments.     */    static RecipeDialogFragment newInstance(int wIDth) {        RecipeDialogFragment fragment = new RecipeDialogFragment();        Bundle args = new Bundle(1);        args.putInt("wIDth", wIDth);        fragment.setArguments(args);        return fragment;    }
总结

以上是内存溢出为你收集整理的android – 如何设置FragmentDialog大小以包装到ListView全部内容,希望文章能够帮你解决android – 如何设置FragmentDialog大小以包装到ListView所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1117250.html

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

发表评论

登录后才能评论

评论列表(0条)

保存