我正在使用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
但是由于某种原因它不能工作。任何帮助都将不胜感激。