It is as simple as the fact that when using the DialogFragment the LayoutParams of the parent view are not been set, thus renders the dialog badly.
After some thinking about it I've came up with this:
In res/values/stylex.xml:
<resources> ... <style name="AlertDialogStyle" > <item name="android:cacheColorHint">@android:color/transparent</item> <item name="android:windowBackground">@android:color/transparent</item> </style> ... </resources>
In res/layout/alert_dialog_body.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/tools" android:id="@+id/xx" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="@android:drawable/alert_dark_frame" android:orientation="vertical" > <TextView android:id="@+id/Title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="10dp" android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" /> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginLeft="2dp" android:layout_marginRight="2dp" android:layout_marginTop="5dp" android:contentDescription="Separator" android:scaleType="fitXY" android:src="@drawable/settings_separator" app:ignore="HardcodedText" /> <TextView android:id="@+id/AlertBody" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="5dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="5dp" android:text="TextView" android:textAppearance="?android:attr/textAppearanceMedium" /> <Button android:id="@+id/DialogButton" style="?android:attr/buttonStyleSmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="10dp" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_marginTop="15dp" android:text="Button" /> </LinearLayout> </RelativeLayout>
(The RelativeLayout is the trick...)
Separator Image |
In res/drawable/... filename.png
and the Dialog fragment:
public class MyDialogFragment extends DialogFragment { protected final String TAG = getClass().getSimpleName(); private int titleId; private int bodyId; private int buttonId; public MyDialogFragment(int titleId, int bodyId, int buttonId) { super(); this.titleId = titleId; this.bodyId = bodyId; this.buttonId = buttonId; } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.alert_dialog_body, container); TextView title = (TextView) view.findViewById(R.id.Title); TextView body = (TextView) view.findViewById(R.id.AlertBody); Button button = (Button) view.findViewById(R.id.DialogButton); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { getDialog().dismiss(); } }); button.setText(buttonId); title.setText(titleId); body.setText(bodyId); return view; } @Override public void onAttach(android.app.Activity activity) { super.onAttach(activity); } @Override public void onCreate(android.os.Bundle savedInstanceState) { super.onCreate(savedInstanceState); setStyle(DialogFragment.STYLE_NO_FRAME, R.style.AlertDialogStyle); } }
And Just in case you would actually want to use this:
MyDialogFragment dialogFragment = new MyDialogFragment(R.string.title,R.string.body,R.string.button_label); dialogFragment .setCancelable(false); dialogFragment .show(getSupportFragmentManager(), "Dialog Fragment");
You can check out the code here.
Leave your comments below... :)
No comments:
Post a Comment