在Android中使用自定义语言环境扩展视图

在Android中使用自定义语言环境扩展视图,第1张

概述我正在使用LayoutInflater膨胀自定义布局以生成发票和收据作为位图,然后将它们发送到打印机或将其导出为png文件,如下所示:LayoutInflaterinflater=LayoutInflater.from(getContext());Viewview=inflater.inflate(R.layout.layout_invoice,null);//Populatetheviewhe

我正在使用LayoutInflater膨胀自定义布局以生成发票和收据作为位图,然后将它们发送到打印机或将其导出为png文件,如下所示:

LayoutInflater inflater = LayoutInflater.from(getContext());VIEw vIEw = inflater.inflate(R.layout.layout_invoice, null);// Populate the vIEw here...int wIDthMeasureSpec = VIEw.MeasureSpec.makeMeasureSpec(paperWIDth, VIEw.MeasureSpec.EXACTLY);int heightmeasureSpec = VIEw.MeasureSpec.makeMeasureSpec(0, VIEw.MeasureSpec.UnspecIFIED);vIEw.measure(wIDthMeasureSpec, heightmeasureSpec);int wIDth = vIEw.getMeasureDWIDth();int height = vIEw.getMeasuredHeight();vIEw.layout(0, 0, wIDth, height);Bitmap reVal = Bitmap.createBitmap(wIDth, height, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(reVal);vIEw.draw(canvas);

现在,此代码可以完美运行,但可以使用设备当前的语言来扩大视图.有时我需要用其他语言生成发票,是否有任何方法可以在自定义语言环境中扩展该视图?

注意:在扩大视图范围之前,我尝试过更改语言环境,然后再将其重置:

Resources resources = context.getResources();Configuration configuration = resources.getConfiguration();configuration.locale = new Locale("fr");// Inflate the vIEw// ...// reset the locale to the original value

但是由于某种原因,它不起作用.任何援助将不胜感激.

解决方法:

您可以使用此简单的类创建本地化的上下文

public class LocalizedContextwrapper extends Contextwrapper {    public LocalizedContextwrapper(Context base) {        super(base);    }    public static Contextwrapper wrap(Context context, Locale locale) {        Configuration configuration = context.getResources().getConfiguration();        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {            configuration.setLocale(locale);            context = context.createConfigurationContext(configuration);        } else {            configuration.locale = locale;            context.getResources().updateConfiguration(                    configuration,                    context.getResources().getdisplayMetrics()            );        }        return new LocalizedContextwrapper(context);    }}

并像这样使用它

Context localizedContext = LocalizedContextwrapper.wrap(context, locale);
总结

以上是内存溢出为你收集整理的在Android中使用自定义语言环境扩展视图全部内容,希望文章能够帮你解决在Android中使用自定义语言环境扩展视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1081495.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-27
下一篇2022-05-27

发表评论

登录后才能评论

评论列表(0条)

    保存