调整DialogFragment宽度和高度

时间:2015-12-07 21:03:33

标签: android size android-dialogfragment

我对DialogFragmnt的{​​{1}}和Width有疑问。这是我的班级代表Height

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; } } 我在此.xml注册的内容:

DialogFragment

问题在于,当我将它的大小设置为600 x 300时,它在我的1280x720设备中显示正常,但是例如当我的朋友在1920x1080分辨率下显示它时,对话框被包装并且仅显示标题。列表被包装,并未完整显示。任何想法我怎么能自动设置它的大小以适应每个显示器并显示整个对话框包含在其内容?

修改

我已经想出要将<?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> 的宽度调整到它的内容,如下所示:

DialogFragment

然而,身高不正常:/

1 个答案:

答案 0 :(得分:4)

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

找到了这个想法