|
19 | 19 | import android.animation.LayoutTransition; |
20 | 20 | import android.app.FragmentManager.BackStackEntry; |
21 | 21 | import android.content.Context; |
| 22 | +import android.content.res.TypedArray; |
22 | 23 | import android.util.AttributeSet; |
| 24 | +import android.view.Gravity; |
23 | 25 | import android.view.LayoutInflater; |
24 | 26 | import android.view.View; |
25 | 27 | import android.view.ViewGroup; |
@@ -51,7 +53,11 @@ public class FragmentBreadCrumbs extends ViewGroup |
51 | 53 | private OnClickListener mParentClickListener; |
52 | 54 |
|
53 | 55 | private OnBreadCrumbClickListener mOnBreadCrumbClickListener; |
54 | | - |
| 56 | + |
| 57 | + private int mGravity; |
| 58 | + |
| 59 | + private static final int DEFAULT_GRAVITY = Gravity.START | Gravity.CENTER_VERTICAL; |
| 60 | + |
55 | 61 | /** |
56 | 62 | * Interface to intercept clicks on the bread crumbs. |
57 | 63 | */ |
@@ -80,6 +86,14 @@ public FragmentBreadCrumbs(Context context, AttributeSet attrs) { |
80 | 86 |
|
81 | 87 | public FragmentBreadCrumbs(Context context, AttributeSet attrs, int defStyle) { |
82 | 88 | super(context, attrs, defStyle); |
| 89 | + |
| 90 | + TypedArray a = context.obtainStyledAttributes(attrs, |
| 91 | + com.android.internal.R.styleable.FragmentBreadCrumbs, defStyle, 0); |
| 92 | + |
| 93 | + mGravity = a.getInt(com.android.internal.R.styleable.FragmentBreadCrumbs_gravity, |
| 94 | + DEFAULT_GRAVITY); |
| 95 | + |
| 96 | + a.recycle(); |
83 | 97 | } |
84 | 98 |
|
85 | 99 | /** |
@@ -159,16 +173,50 @@ public void setTitle(CharSequence title, CharSequence shortTitle) { |
159 | 173 |
|
160 | 174 | @Override |
161 | 175 | protected void onLayout(boolean changed, int l, int t, int r, int b) { |
162 | | - // Eventually we should implement our own layout of the views, |
163 | | - // rather than relying on a linear layout. |
| 176 | + // Eventually we should implement our own layout of the views, rather than relying on |
| 177 | + // a single linear layout. |
164 | 178 | final int childCount = getChildCount(); |
165 | | - for (int i = 0; i < childCount; i++) { |
166 | | - final View child = getChildAt(i); |
| 179 | + if (childCount == 0) { |
| 180 | + return; |
| 181 | + } |
| 182 | + |
| 183 | + final View child = getChildAt(0); |
167 | 184 |
|
168 | | - int childRight = mPaddingLeft + child.getMeasuredWidth() - mPaddingRight; |
169 | | - int childBottom = mPaddingTop + child.getMeasuredHeight() - mPaddingBottom; |
170 | | - child.layout(mPaddingLeft, mPaddingTop, childRight, childBottom); |
| 185 | + final int childTop = mPaddingTop; |
| 186 | + final int childBottom = mPaddingTop + child.getMeasuredHeight() - mPaddingBottom; |
| 187 | + |
| 188 | + int childLeft; |
| 189 | + int childRight; |
| 190 | + |
| 191 | + final int layoutDirection = getLayoutDirection(); |
| 192 | + final int horizontalGravity = mGravity & Gravity.RELATIVE_HORIZONTAL_GRAVITY_MASK; |
| 193 | + switch (Gravity.getAbsoluteGravity(horizontalGravity, layoutDirection)) { |
| 194 | + case Gravity.RIGHT: |
| 195 | + childRight = mRight - mLeft - mPaddingRight; |
| 196 | + childLeft = childRight - child.getMeasuredWidth(); |
| 197 | + break; |
| 198 | + |
| 199 | + case Gravity.CENTER_HORIZONTAL: |
| 200 | + childLeft = mPaddingLeft + (mRight - mLeft - child.getMeasuredWidth()) / 2; |
| 201 | + childRight = childLeft + child.getMeasuredWidth(); |
| 202 | + break; |
| 203 | + |
| 204 | + case Gravity.LEFT: |
| 205 | + default: |
| 206 | + childLeft = mPaddingLeft; |
| 207 | + childRight = childLeft + child.getMeasuredWidth(); |
| 208 | + break; |
171 | 209 | } |
| 210 | + |
| 211 | + if (childLeft < mPaddingLeft) { |
| 212 | + childLeft = mPaddingLeft; |
| 213 | + } |
| 214 | + |
| 215 | + if (childRight > mRight - mLeft - mPaddingRight) { |
| 216 | + childRight = mRight - mLeft - mPaddingRight; |
| 217 | + } |
| 218 | + |
| 219 | + child.layout(childLeft, childTop, childRight, childBottom); |
172 | 220 | } |
173 | 221 |
|
174 | 222 | @Override |
|
0 commit comments