我有一个编辑文本,其图像可绘制,带有不可编辑的前缀编辑文本,但现在我想让它支持 rtl。尽管我很努力,但我还是无法支持 rtl。
我的自定义课程如下,
public class PrefixedEditText extends TextInputEditText {
private String mPrefix = "+"; // can be hardcoded for demo purposes
private Rect mPrefixRect = new Rect(); // actual prefix size
public PrefixedEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
getPaint().getTextBounds(mPrefix, 0, mPrefix.length(), mPrefixRect);
mPrefixRect.right += getPaint().measureText(" "); // add some offset
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mPrefix, super.getCompoundPaddingLeft(), getBaseline(), getPaint());
}
@Override
public int getCompoundPaddingLeft() {
return super.getCompoundPaddingLeft() + mPrefixRect.width();
}
}
我对这个类的xml调用如下,
<cl.dd.ui.PrefixedEditText
style="@style/edittext"
android:id="@+id/etCode"
android:maxLength="3"
android:drawableLeft="@drawable/icon_phone_number"
android:drawableStart="@drawable/icon_phone_number"
android:minWidth="@dimen/dim_img_width"
android:hint="@string/s_login_code"
android:tag="@string/s_login_country_code"
android:inputType="number"/>
您需要确保supportsRtl
在你的中设置为 trueAndroidManifest.xml
<application
...
android:supportsRtl="true">
并设置layoutDirection
to locale
, inherit
or rtl
如果您的目标是 SDK 17+,请在您的布局 xml 中
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layoutDirection="locale">
如果您的目标 SDK 低于 17,则必须创建另一个 res 目录,例如layout-ldrtl
or values-ldrtl
也许可以向您的自定义视图发送一个 RTL 标志。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)