用于可绘制的左侧

用于可绘制的左侧

本文介绍了对自定义editext的RTL支持,用于可绘制的左侧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个edittext,它的图像可绘制,左边带有不可编辑的前缀editext,但是现在我想使其支持rtl.尽管付出了我的努力,我仍然无法支持rtl.

I have an edittext having an image as drawable left with a non-editable prefixed editext but now i wanted to make it to support rtl. Despite my effort I am not able to support 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调用如下,

My xml call of this class is as follows,

<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"/>

推荐答案

您需要确保在AndroidManifest.xml

<application
    ...
    android:supportsRtl="true">

如果要定位SDK 17+,则在布局xml中将layoutDirection设置为localeinheritrtl

And set the layoutDirection to locale, inherit or rtl in your layout xml if you are targeting SDK 17+

<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-ldrtlvalues-ldrtl),并可能向您的自定义视图发送rtl标志.

If your target SDK is below 17, you would have to create another res directory like layout-ldrtl or values-ldrtl and maybe send an rtl flag to your custom view.

这篇关于对自定义editext的RTL支持,用于可绘制的左侧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 15:32