调整DialogFragment的宽度和高度

6
我有一个关于 DialogFragmentWidthHeight 的问题。这是代表 DialogFragment 的类:

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时,在我的1280x720设备上显示正常,但例如当我的朋友在1920x1080分辨率上显示它时,对话框会换行,只显示标题。列表被包裹,没有完全显示。有什么办法可以自动调整它的大小以适应每个显示器并显示完整对话框内容?
编辑
我已经想出了如何调整DialogFragment的宽度以适应其内容:
getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT);
    getDialog().setTitle(getString(R.string.recipe_dialog_title));

然而,高度属性不正常工作:/

使用 https://github.com/orhanobut/dialogplus 这个库非常适合显示对话框。 - Ravi
1
使用现成的库不是问题。我的目标是学习并找到解决方案。 - anton86993
1个回答

7
在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
);

希望这能帮到你。
在此处发现了这个想法:如何设置DialogFragment的宽度和高度?

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接