Skip to content

Commit 6ffbe60

Browse files
Fabrice Di MeglioAndroid (Google) Code Review
authored andcommitted
Merge "Fix bug #7325609 FragmentBreadCrumbs should be RTL-aware" into jb-mr1-dev
2 parents 732d88e + 3cc10f4 commit 6ffbe60

File tree

2 files changed

+62
-8
lines changed

2 files changed

+62
-8
lines changed

core/java/android/app/FragmentBreadCrumbs.java

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import android.animation.LayoutTransition;
2020
import android.app.FragmentManager.BackStackEntry;
2121
import android.content.Context;
22+
import android.content.res.TypedArray;
2223
import android.util.AttributeSet;
24+
import android.view.Gravity;
2325
import android.view.LayoutInflater;
2426
import android.view.View;
2527
import android.view.ViewGroup;
@@ -51,7 +53,11 @@ public class FragmentBreadCrumbs extends ViewGroup
5153
private OnClickListener mParentClickListener;
5254

5355
private OnBreadCrumbClickListener mOnBreadCrumbClickListener;
54-
56+
57+
private int mGravity;
58+
59+
private static final int DEFAULT_GRAVITY = Gravity.START | Gravity.CENTER_VERTICAL;
60+
5561
/**
5662
* Interface to intercept clicks on the bread crumbs.
5763
*/
@@ -80,6 +86,14 @@ public FragmentBreadCrumbs(Context context, AttributeSet attrs) {
8086

8187
public FragmentBreadCrumbs(Context context, AttributeSet attrs, int defStyle) {
8288
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();
8397
}
8498

8599
/**
@@ -159,16 +173,50 @@ public void setTitle(CharSequence title, CharSequence shortTitle) {
159173

160174
@Override
161175
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.
164178
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);
167184

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;
171209
}
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);
172220
}
173221

174222
@Override

core/res/res/values/attrs.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5781,4 +5781,10 @@
57815781
<attr name="leftToRight" format="boolean" />
57825782
</declare-styleable>
57835783

5784+
<!-- Attributes that can be used with <code>&lt;FragmentBreadCrumbs&gt;</code>
5785+
tags. -->
5786+
<declare-styleable name="FragmentBreadCrumbs">
5787+
<attr name="gravity" />
5788+
</declare-styleable>
5789+
57845790
</resources>

0 commit comments

Comments
 (0)