2015-12-07 2 views
3

У меня проблема с DialogFragmntWidth и 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, который я infalte в этом DialogFragment:

<?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 х 300 он отображается нормально в моем 1280x720, но, например, когда мой друг отображает его в диалоговом окне с разрешением 1920x1080, он завершается, и отображается только заголовок. Список завернут и не отображается целиком. Любая идея, как я могу автоматически установить его размер, чтобы он соответствовал каждому дисплею и отображал весь диалог, который был обернут его содержимым?

Редактировать

Я понял, чтобы отрегулировать ширину DialogFragment на его содержание, как это:

getDialog().getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, 
      WindowManager.LayoutParams.WRAP_CONTENT); 
    getDialog().setTitle(getString(R.string.recipe_dialog_title)); 

Однако высота не работает должным образом:/

+0

использование https: // GitHub .com/orhanobut/dialogplus это отличный библиотечный файл для отображения диалогов – Ravi

+1

Не проблема в использовании готовой библиотеки. Моя цель - узнать что-то и найти решение для этого. – anton86993

ответ

2

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

Смежные вопросы