下面列出了android.support.v4.view.ViewCompat#LAYOUT_DIRECTION_RTL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void draw(Canvas canvas) {
copyBounds(mTmpRect);
canvas.save();
// Layout direction must be obtained from the activity.
final boolean isLayoutRTL = ViewCompat.getLayoutDirection(
mActivity.getWindow().getDecorView()) == ViewCompat.LAYOUT_DIRECTION_RTL;
final int flipRtl = isLayoutRTL ? -1 : 1;
final int width = mTmpRect.width();
canvas.translate(-mOffset * width * mPosition * flipRtl, 0);
// Force auto-mirroring if it's not supported by the platform.
if (isLayoutRTL && !mHasMirroring) {
canvas.translate(width, 0);
canvas.scale(-1, 1);
}
super.draw(canvas);
canvas.restore();
}
@Override
protected void onDraw(final Canvas canvas) {
super.onDraw(canvas);
final int layoutDirection = ViewCompat.getLayoutDirection(this);
final int width = getWidth();
final int height = getHeight();
final float halfHeight = height / 2.0f;
final Path path = mIndicatorPath;
path.rewind();
if (layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL) {
// Left arrow
path.moveTo(width, 0.0f);
path.lineTo(0.0f, halfHeight);
path.lineTo(width, height);
} else { // LAYOUT_DIRECTION_LTR
// Right arrow
path.moveTo(0.0f, 0.0f);
path.lineTo(width, halfHeight);
path.lineTo(0.0f, height);
}
path.close();
final int[] stateSet = getDrawableState();
final int color = mIndicatorColor.getColorForState(stateSet, 0);
mIndicatorPaint.setColor(color);
canvas.drawPath(path, mIndicatorPaint);
}
/**
* Return the layout direction for a given Locale
*
* @param locale the Locale for which we want the layout direction. Can be null.
* @return the layout direction. This may be one of:
* {@link ViewCompat#LAYOUT_DIRECTION_LTR} or
* {@link ViewCompat#LAYOUT_DIRECTION_RTL}.
*
* Be careful: this code will need to be updated when vertical scripts will be supported
*/
public static int getLayoutDirectionFromLocale(Locale locale) {
if (locale != null && !locale.equals(ROOT)) {
final String scriptSubtag = ICUCompat.getScript(
ICUCompat.addLikelySubtags(locale.toString()));
if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);
if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
return ViewCompat.LAYOUT_DIRECTION_RTL;
}
}
return ViewCompat.LAYOUT_DIRECTION_LTR;
}
/**
* Return the layout direction for a given Locale
*
* @param locale the Locale for which we want the layout direction. Can be null.
* @return the layout direction. This may be one of:
* {@link ViewCompat#LAYOUT_DIRECTION_LTR} or
* {@link ViewCompat#LAYOUT_DIRECTION_RTL}.
*
* Be careful: this code will need to be updated when vertical scripts will be supported
*/
public static int getLayoutDirectionFromLocale(Locale locale) {
if (locale != null && !locale.equals(ROOT)) {
final String scriptSubtag = ICUCompat.getScript(
ICUCompat.addLikelySubtags(locale.toString()));
if (scriptSubtag == null) return getLayoutDirectionFromFirstChar(locale);
if (scriptSubtag.equalsIgnoreCase(ARAB_SCRIPT_SUBTAG) ||
scriptSubtag.equalsIgnoreCase(HEBR_SCRIPT_SUBTAG)) {
return ViewCompat.LAYOUT_DIRECTION_RTL;
}
}
return ViewCompat.LAYOUT_DIRECTION_LTR;
}
private boolean isLayoutRTL() {
return ViewCompat.getLayoutDirection(mRecyclerView) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
public boolean isLayoutRtl() {
return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
protected boolean isLayoutRTL() {
return getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
@Override
protected boolean defaultIsRtl() {
final int dir = TextUtilsCompat.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
return (dir == ViewCompat.LAYOUT_DIRECTION_RTL);
}
/**
* Used in combination of a TabLayout to create a multi view layout.
* @param views Thew views to use in the pager Adapter.
*/
TabularContextMenuPagerAdapter(List<Pair<String, ViewGroup>> views) {
mViewList = views;
mIsRightToLeft = TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault())
== ViewCompat.LAYOUT_DIRECTION_RTL;
}
public boolean isLayoutRtl() {
return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
protected boolean isLayoutRTL() {
return getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
boolean isLayoutRTL() {
return getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
protected boolean isLayoutRTL() {
return getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
private boolean isDevicePreferredLanguageRTL() {
final int directionality =
TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault());
return directionality == ViewCompat.LAYOUT_DIRECTION_RTL;
}
private boolean isRTL() {
Configuration config = getResources().getConfiguration();
return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
@Override
protected void onDraw(Canvas canvas) {
if (mDividerDrawableVertical == null && mDividerDrawableHorizontal == null) {
return;
}
if (mShowDividerHorizontal == SHOW_DIVIDER_NONE
&& mShowDividerVertical == SHOW_DIVIDER_NONE) {
return;
}
int layoutDirection = ViewCompat.getLayoutDirection(this);
boolean isRtl;
boolean fromBottomToTop = false;
switch (mFlexDirection) {
case FLEX_DIRECTION_ROW:
isRtl = layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL;
if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
fromBottomToTop = true;
}
drawDividersHorizontal(canvas, isRtl, fromBottomToTop);
break;
case FLEX_DIRECTION_ROW_REVERSE:
isRtl = layoutDirection != ViewCompat.LAYOUT_DIRECTION_RTL;
if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
fromBottomToTop = true;
}
drawDividersHorizontal(canvas, isRtl, fromBottomToTop);
break;
case FLEX_DIRECTION_COLUMN:
isRtl = layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL;
if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
isRtl = !isRtl;
}
fromBottomToTop = false;
drawDividersVertical(canvas, isRtl, fromBottomToTop);
break;
case FLEX_DIRECTION_COLUMN_REVERSE:
isRtl = layoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL;
if (mFlexWrap == FLEX_WRAP_WRAP_REVERSE) {
isRtl = !isRtl;
}
fromBottomToTop = true;
drawDividersVertical(canvas, isRtl, fromBottomToTop);
break;
}
}
@Override
protected boolean defaultIsRtl() {
final int dir = TextUtilsCompat.getLayoutDirectionFromLocale(java.util.Locale.getDefault());
return (dir == ViewCompat.LAYOUT_DIRECTION_RTL);
}
public CollapsingTitleLayout(Context context, AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
setWillNotDraw(false);
paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CollapsingTitleLayout);
final boolean isRtl = ViewCompat.getLayoutDirection(this)
== ViewCompat.LAYOUT_DIRECTION_RTL;
// first check if all insets set the same
titleInsetStart = titleInsetEnd = titleInsetBottom =
a.getDimensionPixelSize(R.styleable.CollapsingTitleLayout_titleInset, 0);
titleInsetTop = titleInsetStart;
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetStart)) {
final int insetStart = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetStart, 0);
if (isRtl) {
titleInsetEnd = insetStart;
} else {
titleInsetStart = insetStart;
}
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetTop)) {
titleInsetTop = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetTop, 0);
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetEnd)) {
final int insetEnd = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetEnd, 0);
if (isRtl) {
titleInsetStart = insetEnd;
} else {
titleInsetEnd = insetEnd;
}
}
if (a.hasValue(R.styleable.CollapsingTitleLayout_titleInsetBottom)) {
titleInsetBottom = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_titleInsetBottom, 0);
}
final int textAppearance = a.getResourceId(
R.styleable.CollapsingTitleLayout_android_textAppearance,
android.R.style.TextAppearance);
TypedArray atp = getContext().obtainStyledAttributes(textAppearance,
R.styleable.CollapsingTextAppearance);
paint.setColor(atp.getColor(R.styleable.CollapsingTextAppearance_android_textColor,
Color.WHITE));
collapsedTextSize = atp.getDimensionPixelSize(
R.styleable.CollapsingTextAppearance_android_textSize, 0);
if (atp.hasValue(R.styleable.CollapsingTextAppearance_font)) {
paint.setTypeface(FontUtil.get(getContext(),
atp.getString(R.styleable.CollapsingTextAppearance_font)));
}
atp.recycle();
if (a.hasValue(R.styleable.CollapsingTitleLayout_collapsedTextSize)) {
collapsedTextSize = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_collapsedTextSize, 0);
paint.setTextSize(collapsedTextSize);
}
maxExpandedTextSize = a.getDimensionPixelSize(
R.styleable.CollapsingTitleLayout_maxExpandedTextSize, Integer.MAX_VALUE);
lineHeightHint =
a.getDimensionPixelSize(R.styleable.CollapsingTitleLayout_lineHeightHint, 0);
maxLines = a.getInteger(R.styleable.CollapsingTitleLayout_android_maxLines, 5);
a.recycle();
}
protected boolean isLayoutRTL() {
return getLayoutDirection() == ViewCompat.LAYOUT_DIRECTION_RTL;
}
/**
* Helper method to return true if the Locale directionality is RTL.
*
* @param locale The Locale whose directionality will be checked to be RTL or LTR
* @return true if the {@code locale} directionality is RTL. False otherwise.
*/
private static boolean isRtlLocale(Locale locale) {
return (TextUtilsCompat.getLayoutDirectionFromLocale(locale) == ViewCompat.LAYOUT_DIRECTION_RTL);
}