如何改变DialogFragment的大小

5

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

enter image description here

我试图更改它的大小,但没有成功,在Java类中,我尝试这样做:

getDialog().getWindow().setLayout(800,800); getDialog().setTitle("请告诉我们");

如您所见,以上是代码。

 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个回答

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

例如:

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

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