Skip to content

Commit 10db06c

Browse files
committed
Support Rtl
1 parent bf54114 commit 10db06c

File tree

3 files changed

+30
-26
lines changed

3 files changed

+30
-26
lines changed

library/src/main/java/com/ruffian/library/widget/helper/RBaseHelper.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,17 @@
2525

2626
import androidx.annotation.ColorInt;
2727
import androidx.annotation.StyleableRes;
28+
import androidx.core.text.TextUtilsCompat;
29+
import androidx.core.view.ViewCompat;
2830

2931
import com.ruffian.library.widget.R;
3032
import com.ruffian.library.widget.clip.ClipHelper;
3133
import com.ruffian.library.widget.clip.ClipPathManager;
3234
import com.ruffian.library.widget.clip.IClip;
3335
import com.ruffian.library.widget.shadow.ShadowDrawable;
3436

37+
import java.util.Locale;
38+
3539

3640
/**
3741
* BaseHelper
@@ -1607,4 +1611,13 @@ public void onGlobalLayout() {
16071611
//初始化clip
16081612
initClip();
16091613
}
1614+
1615+
/**
1616+
* 是否从右到左布局
1617+
*
1618+
* @return
1619+
*/
1620+
protected static boolean isRtl() {
1621+
return TextUtilsCompat.getLayoutDirectionFromLocale(Locale.getDefault()) == ViewCompat.LAYOUT_DIRECTION_RTL;
1622+
}
16101623
}

library/src/main/java/com/ruffian/library/widget/helper/RTextViewHelper.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ private void initAttributeSet(Context context, AttributeSet attrs) {
166166
mIconSelected = getDrawable(context, a, R.styleable.RTextView_icon_src_selected);
167167
mIconChecked = getDrawable(context, a, R.styleable.RTextView_icon_src_checked);
168168
//兼容逻辑(优先级 drawableStart > drawableLeft > icon_normal_left)
169-
if (!TextViewUtils.isRight2Left()) {
169+
if (!isRtl()) {
170170
if (drawableStart != null) drawableLeft = drawableStart;
171171
if (drawableEnd != null) drawableRight = drawableEnd;
172172
} else {
@@ -818,8 +818,9 @@ private void setCompoundDrawables(Drawable drawableLeft, Drawable drawableRight,
818818
drawableTop.setBounds(0, 0, mIconWidthTop, mIconHeightTop);
819819
if (drawableBottom != null)
820820
drawableBottom.setBounds(0, 0, mIconWidthBottom, mIconHeightBottom);
821+
boolean rtl = isRtl();
821822
//setDrawable
822-
mView.setCompoundDrawables(drawableLeft, drawableTop, drawableRight, drawableBottom);
823+
mView.setCompoundDrawables(rtl ? drawableRight : drawableLeft, drawableTop, rtl ? drawableLeft : drawableRight, drawableBottom);
823824
}
824825

825826
/**
@@ -834,12 +835,21 @@ private void setCompoundDrawables(Drawable drawableLeft, Drawable drawableRight,
834835
private void setSingleCompoundDrawable(Drawable drawable, int drawableWidth, int drawableHeight, int direction) {
835836
if (drawable != null)
836837
drawable.setBounds(0, 0, drawableWidth, drawableHeight);
837-
mView.setCompoundDrawables(
838-
direction == ICON_DIR_LEFT ? drawable : null,
839-
direction == ICON_DIR_TOP ? drawable : null,
840-
direction == ICON_DIR_RIGHT ? drawable : null,
841-
direction == ICON_DIR_BOTTOM ? drawable : null);
842838

839+
boolean rtl = isRtl();
840+
if (rtl) {
841+
mView.setCompoundDrawables(
842+
direction == ICON_DIR_RIGHT ? drawable : null,
843+
direction == ICON_DIR_TOP ? drawable : null,
844+
direction == ICON_DIR_LEFT ? drawable : null,
845+
direction == ICON_DIR_BOTTOM ? drawable : null);
846+
} else {
847+
mView.setCompoundDrawables(
848+
direction == ICON_DIR_LEFT ? drawable : null,
849+
direction == ICON_DIR_TOP ? drawable : null,
850+
direction == ICON_DIR_RIGHT ? drawable : null,
851+
direction == ICON_DIR_BOTTOM ? drawable : null);
852+
}
843853
}
844854

845855

library/src/main/java/com/ruffian/library/widget/utils/TextViewUtils.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,10 @@
11
package com.ruffian.library.widget.utils;
22

33
import android.graphics.Paint;
4-
import android.os.Build;
5-
import android.text.TextUtils;
6-
import android.view.View;
74
import android.widget.TextView;
85

9-
import androidx.annotation.RequiresApi;
10-
116
import java.util.ArrayList;
127
import java.util.Collections;
13-
import java.util.Locale;
148

159
/**
1610
* TextView工具类
@@ -108,17 +102,4 @@ public float getTextHeight(TextView view, int drawableHeight, int paddingTop, in
108102
return textHeight;
109103
}
110104

111-
/**
112-
* 是否从右到左布局
113-
*
114-
* @return
115-
*/
116-
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
117-
public static boolean isRight2Left() {
118-
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) {
119-
return TextUtils.getLayoutDirectionFromLocale(Locale.getDefault()) == View.LAYOUT_DIRECTION_RTL;
120-
}
121-
return false;
122-
}
123-
124105
}

0 commit comments

Comments
 (0)