使用DialogFragment的对话框未正确显示

时间:2014-07-28 08:29:55

标签: android android-dialogfragment

我正在使用DialogFragment来显示一个对话框。但是对话框显示的是标题。当我没有使用标题时,我的对话框没有正确显示。

带标题的对话框 enter image description here

没有标题的对话

enter image description here

XML

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/share_background"
    >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
       >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Share"
            android:textSize="25sp"
            android:textColor="#fff"
            android:id="@+id/textView"
            android:layout_gravity="center_horizontal" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button2" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/button3" />
    </LinearLayout>

</LinearLayout>

DialogFragment Code

public class ShareDialogFragment extends DialogFragment {

    private View view;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.share_dialog, null, false);
       getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return view;
    }


}

为什么会这样?????

3 个答案:

答案 0 :(得分:3)

试试这个:

<TextView
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Share"
        android:textSize="25sp"
        android:textColor="#fff"
        android:id="@+id/textView"
        android:layout_gravity="center_horizontal" />

希望这有帮助。

答案 1 :(得分:1)

将父线性布局的布局高度和宽度固定为固定值。

示例:

 android:layout_width="200dp"
 android:layout_height="wrap_content"

答案 2 :(得分:0)

试试这个:

inflater.inflate(R.layout.share_dialog, container, false);
相关问题