Keyboard shortcuts: RTL for KeyboardShortcutKeysLayout

Introduced RTL support for the KeyboardShortcutKeysLayout and
with another minor change to the dialog layout achived full
RTL support for the keyboard shortcuts UI.

Bug: 22776761
Bug: 27674152
Change-Id: I14e38dc4533208f6fd982a53a1d0305e003d926b
This commit is contained in:
Andrei Stingaceanu
2016-03-21 11:51:58 +00:00
parent 36e480cf8c
commit 2298bb199c
2 changed files with 43 additions and 11 deletions

View File

@@ -16,7 +16,7 @@
~ limitations under the License ~ limitations under the License
--> -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" <TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="wrap_content"
android:layout_height="match_parent" android:layout_height="match_parent"
android:textSize="14sp" android:textSize="14sp"
android:paddingStart="24dp" android:paddingStart="24dp"

View File

@@ -29,13 +29,16 @@ import android.view.ViewGroup;
*/ */
public final class KeyboardShortcutKeysLayout extends ViewGroup { public final class KeyboardShortcutKeysLayout extends ViewGroup {
private int mLineHeight; private int mLineHeight;
private final Context mContext;
public KeyboardShortcutKeysLayout(Context context) { public KeyboardShortcutKeysLayout(Context context) {
super(context); super(context);
this.mContext = context;
} }
public KeyboardShortcutKeysLayout(Context context, AttributeSet attrs) { public KeyboardShortcutKeysLayout(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.mContext = context;
} }
@Override @Override
@@ -104,7 +107,9 @@ public final class KeyboardShortcutKeysLayout extends ViewGroup {
protected void onLayout(boolean changed, int l, int t, int r, int b) { protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount(); int childCount = getChildCount();
int fullRowWidth = r - l; int fullRowWidth = r - l;
int xPos = getPaddingLeft(); int xPos = isRTL()
? fullRowWidth - getPaddingRight()
: getPaddingLeft();
int yPos = getPaddingTop(); int yPos = getPaddingTop();
int lastHorizontalSpacing = 0; int lastHorizontalSpacing = 0;
// The index of the child which starts the current row. // The index of the child which starts the current row.
@@ -117,18 +122,25 @@ public final class KeyboardShortcutKeysLayout extends ViewGroup {
int currentChildWidth = currentChild.getMeasuredWidth(); int currentChildWidth = currentChild.getMeasuredWidth();
LayoutParams lp = (LayoutParams) currentChild.getLayoutParams(); LayoutParams lp = (LayoutParams) currentChild.getLayoutParams();
// If the current child does not fit on this row. boolean childDoesNotFitOnRow = isRTL()
if (xPos + currentChildWidth > fullRowWidth) { ? xPos - getPaddingLeft() - currentChildWidth < 0
: xPos + currentChildWidth > fullRowWidth;
if (childDoesNotFitOnRow) {
// Layout all the children on this row but the current one. // Layout all the children on this row but the current one.
layoutChildrenOnRow(rowStartIdx, i, fullRowWidth, xPos, yPos, layoutChildrenOnRow(rowStartIdx, i, fullRowWidth, xPos, yPos,
lastHorizontalSpacing); lastHorizontalSpacing);
// Update the positions for starting on the new row. // Update the positions for starting on the new row.
xPos = getPaddingLeft(); xPos = isRTL()
? fullRowWidth - getPaddingRight()
: getPaddingLeft();
yPos += mLineHeight; yPos += mLineHeight;
rowStartIdx = i; rowStartIdx = i;
} }
xPos += currentChildWidth + lp.mHorizontalSpacing; xPos = isRTL()
? xPos - currentChildWidth - lp.mHorizontalSpacing
: xPos + currentChildWidth + lp.mHorizontalSpacing;
lastHorizontalSpacing = lp.mHorizontalSpacing; lastHorizontalSpacing = lp.mHorizontalSpacing;
} }
} }
@@ -148,21 +160,41 @@ public final class KeyboardShortcutKeysLayout extends ViewGroup {
private void layoutChildrenOnRow(int startIndex, int endIndex, int fullRowWidth, int xPos, private void layoutChildrenOnRow(int startIndex, int endIndex, int fullRowWidth, int xPos,
int yPos, int lastHorizontalSpacing) { int yPos, int lastHorizontalSpacing) {
int freeSpace = fullRowWidth - xPos + lastHorizontalSpacing; if (!isRTL()) {
xPos = getPaddingLeft() + freeSpace; xPos = getPaddingLeft() + fullRowWidth - xPos + lastHorizontalSpacing;
}
for (int j = startIndex; j < endIndex; ++j) { for (int j = startIndex; j < endIndex; ++j) {
View currentChild = getChildAt(j); View currentChild = getChildAt(j);
int currentChildWidth = currentChild.getMeasuredWidth();
LayoutParams lp = (LayoutParams) currentChild.getLayoutParams();
if (isRTL() && j == startIndex) {
xPos = fullRowWidth - xPos - getPaddingRight() - currentChildWidth
- lp.mHorizontalSpacing;
}
currentChild.layout( currentChild.layout(
xPos, xPos,
yPos, yPos,
xPos + currentChild.getMeasuredWidth(), xPos + currentChildWidth,
yPos + currentChild.getMeasuredHeight()); yPos + currentChild.getMeasuredHeight());
xPos += currentChild.getMeasuredWidth()
+ ((LayoutParams) currentChild.getLayoutParams()).mHorizontalSpacing; if (isRTL()) {
int nextChildWidth = j < endIndex - 1
? getChildAt(j + 1).getMeasuredWidth()
: 0;
xPos -= nextChildWidth + lp.mHorizontalSpacing;
} else {
xPos += currentChildWidth + lp.mHorizontalSpacing;
}
} }
} }
private boolean isRTL() {
return mContext.getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL;
}
public static class LayoutParams extends ViewGroup.LayoutParams { public static class LayoutParams extends ViewGroup.LayoutParams {
public final int mHorizontalSpacing; public final int mHorizontalSpacing;
public final int mVerticalSpacing; public final int mVerticalSpacing;