下面列出了android.view.View#LAYOUT_DIRECTION_RTL 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
void updateFromConfiguration(Context context, Resources resources, int wPx, int hPx,
int awPx, int ahPx) {
Configuration configuration = resources.getConfiguration();
isLandscape = (configuration.orientation == Configuration.ORIENTATION_LANDSCAPE);
isTablet = resources.getBoolean(R.bool.is_tablet);
isLargeTablet = resources.getBoolean(R.bool.is_large_tablet);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
isLayoutRtl = (configuration.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
} else {
isLayoutRtl = false;
}
widthPx = wPx;
heightPx = hPx;
availableWidthPx = awPx;
availableHeightPx = ahPx;
updateAvailableDimensions(context);
}
/**
* Determines if the text in the supplied layout is displayed right to left.
*
* @param layout The layout to check.
* @return True if the text in the supplied layout is displayed right to left. False otherwise.
*/
public static boolean isRtlText(@Nullable final Layout layout, @NonNull final Resources resources)
{
boolean result = false;
if (layout != null)
{
// Treat align opposite as right to left by default
result = layout.getAlignment() == Layout.Alignment.ALIGN_OPPOSITE;
// If the first character is a right to left character
final boolean textIsRtl = layout.isRtlCharAt(0);
// If the text and result are right to left then false otherwise use the textIsRtl value
result = (!(result && textIsRtl) && !(!result && !textIsRtl)) || textIsRtl;
if (!result && layout.getAlignment() == Layout.Alignment.ALIGN_NORMAL
&& Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
// If the layout and text are right to left and the alignment is normal then rtl
result = resources.getConfiguration()
.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
else if (layout.getAlignment() == Layout.Alignment.ALIGN_OPPOSITE && textIsRtl)
{
result = false;
}
}
return result;
}
@Override
public void onRtlPropertiesChanged(int layoutDirection) {
super.onRtlPropertiesChanged(layoutDirection);
int viewCompatLayoutDirection = layoutDirection == View.LAYOUT_DIRECTION_RTL ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR;
if (viewCompatLayoutDirection != mLayoutDirection) {
PagerAdapter adapter = super.getAdapter();
int position = 0;
if (adapter != null) {
position = getCurrentItem();
}
mLayoutDirection = viewCompatLayoutDirection;
if (adapter != null) {
adapter.notifyDataSetChanged();
setCurrentItem(position);
}
}
}
private static boolean isLayoutRtl(View view) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
} else {
// All layouts are LTR before JB MR1.
return false;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isRtl(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return false;
} else {
return context.getResources().getConfiguration().getLayoutDirection()
== View.LAYOUT_DIRECTION_RTL;
}
}
boolean isLayoutRtl() {
if (Build.VERSION.SDK_INT >= 17) {
return mAppContext.getResources().getConfiguration()
.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
} else {
// There is only LTR before SDK 17.
return false;
}
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private void setPivotXToTitle() {
Configuration config = getResources().getConfiguration();
if (Build.VERSION_CODES.JELLY_BEAN_MR1 <= Build.VERSION.SDK_INT
&& config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
ViewHelper.setPivotX(mTitleView, findViewById(android.R.id.content).getWidth());
} else {
ViewHelper.setPivotX(mTitleView, 0);
}
}
/**
* 23 * Returns true if view's layout direction is right-to-left.
* 24 *
* 25 * @param view the View whose layout is being considered
* 26
*/
public static boolean isLayoutRtl(View view) {
if (Build.VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
} else {
// All layouts are LTR before JB MR1.
return false;
}
}
private boolean isLeftToRight() {
// If we are pre JB assume always LTR
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return true;
}
// Other methods, seemingly broken when testing though.
// return ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL
// return !ViewUtils.isLayoutRtl(this);
Configuration config = getResources().getConfiguration();
return !(config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
}
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
private boolean isRTL() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
return false;
}
Configuration config = getResources().getConfiguration();
return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
@SuppressLint("RtlHardcoded")
public static void setTextViewGravityStart(final @NonNull TextView textView, @NonNull Context context) {
if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN_MR1) {
if (DynamicLanguage.getLayoutDirection(context) == View.LAYOUT_DIRECTION_RTL) {
textView.setGravity(Gravity.RIGHT);
} else {
textView.setGravity(Gravity.LEFT);
}
}
}
/**
* @return true if the system default layout direction is RTL, false otherwise.
* RTL layout support is from Jelly Bean MR1, so if the version is lower
* than that, it is always false.
*/
public static boolean isSystemLayoutDirectionRtl() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault())
== View.LAYOUT_DIRECTION_RTL;
}
return false;
}
@VisibleForTesting
static boolean isLayoutDirectionRTL(final Context context) {
ApplicationInfo applicationInfo = context.getApplicationInfo();
if ((SDK_INT >= JELLY_BEAN_MR1)
&& (applicationInfo.flags & ApplicationInfo.FLAG_SUPPORTS_RTL) != 0) {
int layoutDirection = getLayoutDirection(context);
return layoutDirection == View.LAYOUT_DIRECTION_RTL;
}
return false;
}
private static float getSignFromDirection(@NonNull View view) {
return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL ? -1f : 1f;
}
private boolean isRtl() {
return (getLayoutDirection() == View.LAYOUT_DIRECTION_RTL);
}
protected boolean isRTL() {
Configuration config = getResources().getConfiguration();
return config.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
@Override
public boolean isRtl(@NonNull View view) {
return view.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL;
}
/**
* Check if the current language is RTL
*
* @param context Context
* @return Result
*/
static boolean isRtlLayout(Context context) {
return context.getResources().getConfiguration().getLayoutDirection() == View.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 (TextUtils.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL);
}
/**
* Return the layout direction. Will be either {@link View#LAYOUT_DIRECTION_LTR} or
* {@link View#LAYOUT_DIRECTION_RTL}.
*
* @return Returns {@link View#LAYOUT_DIRECTION_RTL} if the configuration
* is {@link #SCREENLAYOUT_LAYOUTDIR_RTL}, otherwise {@link View#LAYOUT_DIRECTION_LTR}.
*/
public int getLayoutDirection() {
return (screenLayout&SCREENLAYOUT_LAYOUTDIR_MASK) == SCREENLAYOUT_LAYOUTDIR_RTL
? View.LAYOUT_DIRECTION_RTL : View.LAYOUT_DIRECTION_LTR;
}