我在我的项目中使用DialogFragment,而且我正在禁用Title
getDialog().getwindow().requestFeature(Window.FEATURE_NO_TITLE);
但我的对话已经变得歪曲了.我想保持对话框原样并删除标题.我能怎么做?
<?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="wrap_content" android:background="#ff2d35ff"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginRight="39dp" android:layout_marginLeft="39dp" android:layout_gravity="center_horizontal" android:background="#ff6cff23"> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ItemName" android:id="@+id/itemName" android:layout_weight="1" android:background="#ffd861ff"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ItemPrice" android:id="@+id/itemPrice" android:layout_weight="1" android:background="#ff2ff8ff"/> </LinearLayout> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/itemImage" android:layout_gravity="center_horizontal"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="some test" android:id="@+id/textView9" android:layout_gravity="center_horizontal"/> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" android:background="#ffffff2c"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="-" android:id="@+id/itemminus" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="1" android:id="@+id/textView11" android:layout_weight="1" android:gravity="center_horizontal"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="+" android:id="@+id/itemPlus"/> </LinearLayout> </LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="fill_parent" android:layout_gravity="center_horizontal" android:gravity="center_horizontal"> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemAdd" android:src="@drawable/positive" android:background="@android:color/transparent"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/itemCancel" android:src="@drawable/negative" android:background="@android:color/transparent"/> </LinearLayout>
我意识到当我删除Title对话框时找不到match_parent
解决方法
您必须在dialogfragment类中重写onCreateDialog方法.
@Override public Dialog onCreateDialog(final Bundle savedInstanceState) { RelativeLayout root = new RelativeLayout(getActivity()); root.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)); final Dialog dialog = new Dialog(getActivity()); dialog.requestwindowFeature(Window.FEATURE_NO_TITLE); dialog.setContentView(root); dialog.getwindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); dialog.getwindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT); return dialog; }