没有标题的DialogFragment会缩小对话框宽度

时间:2016-11-11 14:03:47

标签: java android xml user-interface material-design

目标是创建一个显示文件列表的对话框。

问题是,一旦我删除标题(通过任何方式,包括窗口功能,设置样式,使用styles.xml ...),对话框窗口将转换为wrap_content约束。

我在布局中设置的属性不会影响这一点。

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

    <TextView
        android:id="@+id/dialog_files_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="8dp"
        android:padding="8dp"
        android:text="@string/dialog_files_title"
        android:textColor="@color/colorTextActive"
        android:textAppearance="@style/Base.TextAppearance.AppCompat.Title" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/dialog_files_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp" />

</LinearLayout>

我的java代码

public Dialog onCreateDialog(Bundle savedInstanceState) {

        Dialog dialog = super.onCreateDialog(savedInstanceState);

        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);

        return dialog;
    }

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

        @SuppressLint("InflateParams") View view = inflater.inflate(R.layout.dialog_files, null);

        paths = new ArrayList<>();
        recursiveScan(Environment.getExternalStorageDirectory());
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dialog_files_recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(getActivity(), 3);
        AdapterFiles adapter = new AdapterFiles(getActivity(), paths);

        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);

        return view;

    }

结果就是这个

dialog

2 个答案:

答案 0 :(得分:13)

问题似乎是在使用LinearLayout作为根布局时。我切换到RelativeLayout所有适合的地方。在对话框中使用LinearLayout作为根布局并删除标题时,这可能是个错误吗?

答案 1 :(得分:0)

Dialog dialog = super.onCreateDialog(savedInstanceState);

WindowManager.LayoutParams lp = new WindowManager.LayoutParams();

Window window = dialog.getWindow();

if (window != null) {
      window.requestFeature(Window.FEATURE_NO_TITLE);
      window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
}

return dialog;

试试这个。