Browse Source

Items gravity should be 100% functional

Aidan Follestad 10 years ago
parent
commit
1c98f4ff4c

+ 10 - 6
library/src/main/java/com/afollestad/materialdialogs/MaterialDialog.java

@@ -711,18 +711,22 @@ public class MaterialDialog extends DialogBase implements
                 } else {
                     if (mBuilder.positiveText != null && positiveButton.getVisibility() == View.VISIBLE) {
                         // There's a positive button, it goes next to that
-                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                             params.addRule(mBuilder.buttonsGravity == GravityEnum.START ?
                                     RelativeLayout.START_OF : RelativeLayout.END_OF, R.id.buttonDefaultPositive);
-                        else
+                        } else {
                             params.addRule(mBuilder.buttonsGravity == GravityEnum.START ?
                                     RelativeLayout.LEFT_OF : RelativeLayout.RIGHT_OF, R.id.buttonDefaultPositive);
+                        }
                     } else {
                         // Negative button replaces positive button position if there's no positive button
-                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
-                            params.addRule(RelativeLayout.ALIGN_PARENT_END);
-                        else
-                            params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
+                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
+                            params.addRule(mBuilder.buttonsGravity == GravityEnum.START ?
+                                    RelativeLayout.ALIGN_PARENT_END : RelativeLayout.ALIGN_PARENT_START);
+                        } else {
+                            params.addRule(mBuilder.buttonsGravity == GravityEnum.START ?
+                                    RelativeLayout.ALIGN_PARENT_RIGHT : RelativeLayout.ALIGN_PARENT_LEFT);
+                        }
                     }
                 }
                 negativeButton.setLayoutParams(params);

+ 39 - 64
library/src/main/java/com/afollestad/materialdialogs/MaterialDialogAdapter.java

@@ -9,9 +9,9 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.ArrayAdapter;
 import android.widget.CheckBox;
+import android.widget.CompoundButton;
 import android.widget.LinearLayout;
 import android.widget.RadioButton;
-import android.widget.RelativeLayout;
 import android.widget.TextView;
 
 import com.afollestad.materialdialogs.util.DialogUtils;
@@ -62,76 +62,51 @@ class MaterialDialogAdapter extends ArrayAdapter<CharSequence> {
         tv.setTextColor(itemColor);
         dialog.setTypeface(tv, dialog.mBuilder.regularFont);
         view.setTag(index + ":" + dialog.mBuilder.items[index]);
-        setupGravity(view);
-        return view;
-    }
+        setupGravity((ViewGroup) view);
 
-    private void setupGravity(View view) {
-        if (view instanceof LinearLayout) {
-            // Basic list item
-            final LinearLayout itemRoot = (LinearLayout) view;
-            final int gravityInt = MaterialDialog.gravityEnumToGravity(itemGravity);
-            itemRoot.setGravity(gravityInt | Gravity.CENTER_VERTICAL);
-            for (int i = 0; i < itemRoot.getChildCount(); i++)
-                ((LinearLayout.LayoutParams) itemRoot.getChildAt(i).getLayoutParams()).gravity = gravityInt;
-        } else {
-            // Choice list item
-            final RelativeLayout itemRoot = (RelativeLayout) view;
-            for (int i = 0; i < itemRoot.getChildCount(); i++)
-                setupGravityRelative(itemRoot.getChildAt(i), i);
+        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
+            ViewGroup group = (ViewGroup) view;
+            if (group.getChildCount() == 2) {
+                // Remove circular selector from check boxes and radio buttons on Lollipop
+                if (group.getChildAt(0) instanceof CompoundButton)
+                    group.getChildAt(0).setBackground(null);
+                else if (group.getChildAt(1) instanceof CompoundButton)
+                    group.getChildAt(1).setBackground(null);
+            }
         }
+
+        return view;
     }
 
-    private void setupGravityRelative(View child, int index) {
-        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) child.getLayoutParams();
-
-        // Layout alignment
-        if (itemGravity == GravityEnum.CENTER) {
-            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
-        } else if (index == 0) {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
-                params.addRule(itemGravity == GravityEnum.START ?
-                        RelativeLayout.ALIGN_PARENT_START : RelativeLayout.ALIGN_PARENT_END);
-            } else {
-                params.addRule(itemGravity == GravityEnum.START ?
-                        RelativeLayout.ALIGN_PARENT_LEFT : RelativeLayout.ALIGN_PARENT_RIGHT);
-            }
-        }
+    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
+    private void setupGravity(ViewGroup view) {
+        final LinearLayout itemRoot = (LinearLayout) view;
+        final int gravityInt = MaterialDialog.gravityEnumToGravity(itemGravity);
+        itemRoot.setGravity(gravityInt | Gravity.CENTER_VERTICAL);
 
-        if (index == 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            // Removes the circular background selector from checkboxes or radio buttons on Lollipop
-            child.setBackground(null);
-        }
+        if (view.getChildCount() == 2) {
+            if (itemGravity == GravityEnum.END && !isRTL() && view.getChildAt(0) instanceof CompoundButton) {
+                CompoundButton first = (CompoundButton) view.getChildAt(0);
+                view.removeView(first);
 
-        // Relative positioning
-        if (index == 1) {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
-                params.addRule(itemGravity == GravityEnum.START || itemGravity == GravityEnum.CENTER ?
-                        RelativeLayout.END_OF : RelativeLayout.START_OF, R.id.control);
-            } else {
-                params.addRule(itemGravity == GravityEnum.START || itemGravity == GravityEnum.CENTER ?
-                        RelativeLayout.RIGHT_OF : RelativeLayout.LEFT_OF, R.id.control);
-            }
-        }
+                TextView second = (TextView) view.getChildAt(0);
+                view.removeView(second);
+                second.setPadding(second.getPaddingRight(), second.getPaddingTop(),
+                        second.getPaddingLeft(), second.getPaddingBottom());
 
-        // Margin
-        final int frameMargin = (int) getContext().getResources().getDimension(R.dimen.md_dialog_frame_margin);
-        final int controlMargin = (int) getContext().getResources().getDimension(R.dimen.md_listitem_control_margin);
-        if (index == 0) {
-            if (itemGravity == GravityEnum.START || itemGravity == GravityEnum.CENTER) {
-                params.leftMargin = !isRTL() ? frameMargin : controlMargin;
-                params.rightMargin = isRTL() ? frameMargin : controlMargin;
-            } else {
-                params.leftMargin = !isRTL() ? controlMargin : frameMargin;
-                params.rightMargin = isRTL() ? controlMargin : frameMargin;
-            }
-        } else {
-            if (itemGravity == GravityEnum.START || itemGravity == GravityEnum.CENTER) {
-                params.leftMargin = !isRTL() ? 0 : frameMargin;
-                params.rightMargin = isRTL() ? frameMargin : 0;
-            } else {
-                params.leftMargin = !isRTL() ? frameMargin : 0;
-                params.rightMargin = isRTL() ? 0 : frameMargin;
+                view.addView(second);
+                view.addView(first);
+            } else if (itemGravity == GravityEnum.START && isRTL() && view.getChildAt(1) instanceof CompoundButton) {
+                CompoundButton first = (CompoundButton) view.getChildAt(1);
+                view.removeView(first);
+
+                TextView second = (TextView) view.getChildAt(0);
+                view.removeView(second);
+                second.setPadding(second.getPaddingRight(), second.getPaddingTop(),
+                        second.getPaddingRight(), second.getPaddingBottom());
+
+                view.addView(first);
+                view.addView(second);
             }
         }
     }

+ 35 - 0
library/src/main/res/layout-ldrtl/md_listitem_multichoice.xml

@@ -0,0 +1,35 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:descendantFocusability="blocksDescendants"
+    tools:gravity="start|center_vertical"
+    android:paddingLeft="@dimen/md_dialog_frame_margin"
+    android:paddingStart="@dimen/md_dialog_frame_margin"
+    android:paddingRight="@dimen/md_dialog_frame_margin"
+    android:paddingEnd="@dimen/md_dialog_frame_margin">
+
+    <TextView
+        android:id="@+id/title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:minHeight="@dimen/md_listitem_height"
+        tools:text="Item"
+        android:gravity="center_vertical"
+        android:textSize="@dimen/md_listitem_textsize"
+        android:paddingRight="@dimen/md_listitem_control_margin"
+        android:paddingEnd="@dimen/md_listitem_control_margin"
+        tools:ignore="RtlSymmetry" />
+
+    <CheckBox
+        android:id="@+id/control"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:minHeight="@dimen/md_listitem_height"
+        android:clickable="false"
+        android:gravity="center_vertical"
+        android:focusable="false"
+        android:focusableInTouchMode="false" />
+
+</LinearLayout>

+ 35 - 0
library/src/main/res/layout-ldrtl/md_listitem_singlechoice.xml

@@ -0,0 +1,35 @@
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
+    android:orientation="horizontal"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:descendantFocusability="blocksDescendants"
+    tools:gravity="start|center_vertical"
+    android:paddingLeft="@dimen/md_dialog_frame_margin"
+    android:paddingStart="@dimen/md_dialog_frame_margin"
+    android:paddingRight="@dimen/md_dialog_frame_margin"
+    android:paddingEnd="@dimen/md_dialog_frame_margin">
+
+    <TextView
+        android:id="@+id/title"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:minHeight="@dimen/md_listitem_height"
+        tools:text="Item"
+        android:gravity="center_vertical"
+        android:textSize="@dimen/md_listitem_textsize"
+        android:paddingRight="@dimen/md_listitem_control_margin"
+        android:paddingEnd="@dimen/md_listitem_control_margin"
+        tools:ignore="RtlSymmetry" />
+
+    <RadioButton
+        android:id="@+id/control"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:minHeight="@dimen/md_listitem_height"
+        android:clickable="false"
+        android:gravity="center_vertical"
+        android:focusable="false"
+        android:focusableInTouchMode="false" />
+
+</LinearLayout>

+ 11 - 16
library/src/main/res/layout/md_listitem_multichoice.xml

@@ -1,10 +1,14 @@
-<RelativeLayout
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:descendantFocusability="blocksDescendants"
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools">
+    tools:gravity="start|center_vertical"
+    android:paddingLeft="@dimen/md_dialog_frame_margin"
+    android:paddingStart="@dimen/md_dialog_frame_margin"
+    android:paddingRight="@dimen/md_dialog_frame_margin"
+    android:paddingEnd="@dimen/md_dialog_frame_margin">
 
     <CheckBox
         android:id="@+id/control"
@@ -13,13 +17,6 @@
         android:minHeight="@dimen/md_listitem_height"
         android:clickable="false"
         android:gravity="center_vertical"
-        android:layout_centerVertical="true"
-        tools:layout_alignParentLeft="true"
-        tools:layout_alignParentStart="true"
-        tools:layout_marginLeft="@dimen/md_dialog_frame_margin"
-        tools:layout_marginStart="@dimen/md_dialog_frame_margin"
-        tools:layout_marginRight="@dimen/md_listitem_control_margin"
-        tools:layout_marginEnd="@dimen/md_listitem_control_margin"
         android:focusable="false"
         android:focusableInTouchMode="false" />
 
@@ -31,10 +28,8 @@
         tools:text="Item"
         android:gravity="center_vertical"
         android:textSize="@dimen/md_listitem_textsize"
-        android:layout_centerVertical="true"
-        tools:layout_toRightOf="@+id/control"
-        tools:layout_toEndOf="@+id/control"
-        tools:layout_marginRight="@dimen/md_dialog_frame_margin"
-        tools:layout_marginEnd="@dimen/md_dialog_frame_margin" />
+        android:paddingLeft="@dimen/md_listitem_control_margin"
+        android:paddingStart="@dimen/md_listitem_control_margin"
+        tools:ignore="NewApi,RtlSymmetry" />
 
-</RelativeLayout>
+</LinearLayout>

+ 11 - 16
library/src/main/res/layout/md_listitem_singlechoice.xml

@@ -1,10 +1,14 @@
-<RelativeLayout
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    xmlns:tools="http://schemas.android.com/tools"
     android:orientation="horizontal"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:descendantFocusability="blocksDescendants"
-    xmlns:android="http://schemas.android.com/apk/res/android"
-    xmlns:tools="http://schemas.android.com/tools">
+    tools:gravity="start|center_vertical"
+    android:paddingLeft="@dimen/md_dialog_frame_margin"
+    android:paddingStart="@dimen/md_dialog_frame_margin"
+    android:paddingRight="@dimen/md_dialog_frame_margin"
+    android:paddingEnd="@dimen/md_dialog_frame_margin">
 
     <RadioButton
         android:id="@+id/control"
@@ -13,13 +17,6 @@
         android:minHeight="@dimen/md_listitem_height"
         android:clickable="false"
         android:gravity="center_vertical"
-        android:layout_centerVertical="true"
-        tools:layout_alignParentLeft="true"
-        tools:layout_alignParentStart="true"
-        tools:layout_marginLeft="@dimen/md_dialog_frame_margin"
-        tools:layout_marginStart="@dimen/md_dialog_frame_margin"
-        tools:layout_marginRight="@dimen/md_listitem_control_margin"
-        tools:layout_marginEnd="@dimen/md_listitem_control_margin"
         android:focusable="false"
         android:focusableInTouchMode="false" />
 
@@ -31,10 +28,8 @@
         tools:text="Item"
         android:gravity="center_vertical"
         android:textSize="@dimen/md_listitem_textsize"
-        android:layout_centerVertical="true"
-        tools:layout_toRightOf="@+id/control"
-        tools:layout_toEndOf="@+id/control"
-        tools:layout_marginRight="@dimen/md_dialog_frame_margin"
-        tools:layout_marginEnd="@dimen/md_dialog_frame_margin" />
+        android:paddingLeft="@dimen/md_listitem_control_margin"
+        android:paddingStart="@dimen/md_listitem_control_margin"
+        tools:ignore="NewApi,RtlSymmetry" />
 
-</RelativeLayout>
+</LinearLayout>