Tôi đang sử dụng DialogFragment và thấy chiều rộng luôn luôn trở thành toàn màn hình khi tôi tạo layout_width = match_parent bên trong LinearLayout, bất kể có bao nhiêu contraints tôi cố gắng ép buộc nó bằng cách sửa layout_width và minWidth về quan điểm cha mẹ, hoặc thiết lập các dimens Layout trong createView()Kích thước DialogFragment (width) bỏ qua
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/dialog_width"
android:layout_height="@dimen/dialog_height"
android:minWidth="@dimen/dialog_width"
android:minHeight="@dimen/dialog_height"
android:orientation="vertical"
android:paddingTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
...
</LinearLayout>
</LinearLayout>
Dialog.java
public class MyDialog extends DialogFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
...
Resources r = getActivity().getResources();
getDialog().getWindow().setLayout((int)r.getDimension(R.dimen.dialog_width), (int)r.getDimension(R.dimen.dialog_height));
return view;
}
}
các o cách nly tôi có thể làm công việc này là bằng cách thiết lập LinearLayout layout_width bên trong = "@ dimen/dialog_width
hack dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
... >
<LinearLayout
android:layout_width="@dimen/dialog_width"
android:layout_height="wrap_content">
...
</LinearLayout>
...
</LinearLayout>
Những công trình okay, nhưng có một số hậu quả đối với các bộ phận khác của hộp thoại. Đây có phải là lỗi trong Android không?
Sử dụng RelativeLayout thay vì tuyến tính – Sameer