我正在寻找一种更清晰的方式(在Spring 3.2中)获取当前语言环境,而不是在每个Controller方法的开头显式调用LocaleContextHolder.getLocale().它必须与Java注释兼容,因为我没有使用XML配置.这就是我目前正在做的事情.
@Controller
public class WifeController {
@Autowired
private MessageSource msgSrc;
@RequestMapping(value = "/wife/mood")
public String readWife(Model model, @RequestParam("whatImDoing") String iAm) {
Locale loc = LocaleContextHolder.getLocale();
if(iAm.equals("playingXbox")) {
model.addAttribute( "statusTitle", msgSrc.getMessage("mood.angry", null, loc) );
model.addAttribute( "statusDetail", msgSrc.getMessage("mood.angry.xboxdiatribe", null, loc) );
}
return "moodResult";
}
}
Run Code Online (Sandbox Code Playgroud) 任何人都可以建议一种方法来改进这个API8的例子吗?虽然他们说这些视图可以用XML定义,但他们实际上做的是用java编写代码.我明白为什么他们想要.他们已经将一些成员添加到扩展的LinearLayout中,并且这些值是在运行时确定的.
根据哦,宇宙中的每个人,布局指令应该转移到XML.但是对于这个应用程序,在运行时逻辑中保持原样设置是有意义的.所以我们有一种混合方法.膨胀视图,然后填充动态文本.我无法弄清楚如何完成它.这是源头和我尝试过的.
private class SpeechView extends LinearLayout {
public SpeechView(Context context, String title, String words) {
super(context);
this.setOrientation(VERTICAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
...
Run Code Online (Sandbox Code Playgroud)
我想因为LinearLayout有一个android:id ="@ + id/LinearLayout01",我应该能够在OnCreate中做到这一点
SpeechView sv = (SpeechView) findViewById(R.id.LinearLayout01);
Run Code Online (Sandbox Code Playgroud)
但它永远不会击中我添加的最小构造函数:
public class SpeechView extends LinearLayout {
public SpeechView(Context context) {
super(context);
System.out.println("Instantiated SpeechView(Context context)");
}
...
Run Code Online (Sandbox Code Playgroud) android android-custom-view android-layout android-linearlayout