如何更改DialogFragment大小

时间:2016-09-19 01:26:40

标签: android android-dialogfragment

我的Android应用程序中有一个dialogFragment,其中包含一些视图,如下图所示:

enter image description here

我试图改变它的大小而没有成功,在Java类中我试图这样做: getDialog().getWindow().setLayout(800,800); getDialog().setTitle("Please tell us");,如下面的代码所示:

 public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Ini ");

    View view =  inflater.inflate(R.layout.dialog_box_layout, container, false);

    ageSpinner = (Spinner) view.findViewById(R.id.age_spinner_dialog);
    ageSpinner.setSelected(false);
    ageSpinner.setAdapter(populateAgeSpinner());
    ageSpinner.setOnItemSelectedListener(this);

    radioDialogMale = (RadioButton) view.findViewById(R.id.radioDialogM);
    radioDialogMale.setSelected(false);
    radioDialogMale.setOnClickListener(this);
    radioDialogFemale = (RadioButton) view.findViewById(R.id.radioDialogF);
    radioDialogFemale.setSelected(false);
    radioDialogFemale.setOnClickListener(this);


    okButton = (Button) view.findViewById(R.id.dialog_ok);
    okButton.setOnClickListener(this);

    getDialog().getWindow().setLayout(800,800);
    getDialog().setTitle("Please tell us");

    communicator =  (LoginActivity) getActivity();

    setCancelable(false);

    Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Fi ");

    return view;
}

但没有任何改变。

这是布局文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000">


   <!-- <TextView
        android:id="@+id/dialog_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Please tell us your age and gender"
        android:textSize="22dp"
        android:layout_marginTop="15dp"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:textColor="#FFF"
        android:textStyle="bold"
        android:layout_marginBottom="30dp"/> -->


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:text="Age"
        android:textSize="18dp"
        android:gravity="center"/>



    <Spinner
        android:id="@+id/age_spinner_dialog"
        android:layout_width="80dp"
        android:spinnerMode="dropdown"
        android:layout_height="30dp"
        android:layout_marginLeft="18pt"
        android:foregroundTint="@color/com_facebook_button_send_background_color"
        android:backgroundTint="#FFF"/>


    <TextView
        android:id="@+id/text_gender"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50pt"
        android:text="Gender"
        android:textColor="#FFF"
        android:textSize="18dp"
        android:layout_alignTop="@+id/radioGroup"
        android:layout_toEndOf="@+id/age_spinner" />


    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="0.70"
        android:layout_marginEnd="25dp"
        android:layout_marginLeft="20dp"
        android:id="@+id/radioGroup"
        android:layout_alignParentEnd="true">

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="25dp"
            android:text="M"
            android:layout_marginLeft="12dp"
            android:id="@+id/radioDialogM"
            android:layout_marginBottom="30dp"
            android:textColor="@color/com_facebook_button_send_background_color"
            android:backgroundTint="@color/com_facebook_button_send_background_color"
            android:buttonTint="@color/com_facebook_button_send_background_color"/>

        <RadioButton
            android:layout_width="50dp"
            android:layout_height="25dp"
            android:text="F"
            android:id="@+id/radioDialogF"
            android:layout_marginRight="5pt"
            android:textColor="@color/com_facebook_button_send_background_color"
            android:backgroundTint="@color/com_facebook_button_send_background_color"
            android:buttonTint="@color/com_facebook_button_send_background_color"/>

    </RadioGroup>

    />

    <Button
       android:layout_width="wrap_content"
       android:layout_height="30dp"
       android:id="@+id/dialog_ok"
       android:background="@color/com_facebook_button_send_background_color"
        android:textColor="#FFF"
       android:text="OK"
        android:layout_below="@+id/radioGroup"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25dp" />

</RelativeLayout>
请帮忙!!!

1 个答案:

答案 0 :(得分:1)

您可以尝试从onResume()方法设置对话框片段的宽度和高度。如下所示:

public void onResume()
{
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(width, height);
    window.setGravity(Gravity.CENTER);
    //TODO:
}

我在这里找到了解决方案:Setting the size of a DialogFragment