Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何设置FragmentDialog大小以包装到ListView大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有RecyclerView,当我长时间点击其中一个项目FragmentDialog时出现自定义布局:| @ text |.问题是Fragment Dialog不会将区域仅包含在那两个元素中,而是占据几乎整个屏幕的位置.即使我设置布局以包装内容,问题仍然出现.这是问题所在:

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

这是RecyclerView custom_item XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://scheR_915_11845@as.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="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://scheR_915_11845@as.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(@R_607_3868@,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()中执行以下操作

@Override
public 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所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。