坑爹的百分号啊。
今天leader拉了一个分支项目,我从SVN取下来后,就一直全红,还以为谁把错误的classpath传上去了呢,后来一看,R文件死活不重新生成。从console中看出,报了一个下面的错
Multiple annotations found at this line:
- error: Multiple substitutions specified in non-positional format; did you mean to add the formatted="false" attribute?
上网一查发现(转):
反复检查后发现是string.xml中的 % 导致编译失败,
去Google的Android开发者讨论组查看关于这个问题的讨论 后看到了Xavier Ducrohet大神的回复,说这是由于新的SDK采用了新版本的aapt(Android项目编译器),这个版本的aapt编译起来会比老版本更加的严格,然后在Android最新的开发文档的描述String的部分,已经说明了 如何去设置 %s 等符号,下面是文档片段:
If you need to format your strings using String.format(String, Object...) , then you can do so by putting your format arguments in the string resource. For example, with the following resource:
如果你需要使用 String.format(String, Object...) 来格式化你的字符串,你可以把格式化参数放在你的字符串中,参见下面的例子:
<string name="welcome_messages">Hello, %1$s! You have %2$d new messages.</string>
In this example, the format string has two arguments: %1$s is a string and %2$d is a decimal number. You can format the string with arguements from your application like this:
在这个例子中,这个格式化的字符串有2个参数, %1$s是个字符串 %2$d 是个浮点数,你可以在你的程序中按照下面的方法来根据参数来格式化字符串:
Resources res = getResources();
String text = String.format(res.getString(R.string.welcome_messages), username, mailCount);
那么根据例子上说的我需要把%s换成%1$s才行了,修改后编译通过,程序成功启动。
问题补充:
有读者问如何在<string></string>中使用%号
有两个办法可供选择
炫彩屏保是Android系统中的屏幕保护程序,它可以在您不需要观看屏幕的时候呈现不同的炫彩图片,既美观又大方,在让系统省电同时还可以保护您的隐私.
需求要只显示月和日的日历控件,又不想自定义控件,最简单的办法就是隐藏显示年的这个框了,但DatePickerDialog并没有直接提供方法来操作,这里分享一个笨办法:)
效果图:
默认
处理后
代码片段1
/**
* 从当前Dialog中查找DatePicker子控件
*
* @param group
* @return
*/
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}
代码说明:
通过断点也看到Dialog的ContentView里有DatePicker子控件,这里通过遍历的办法来查找这个控件。
使用代码
final Calendar cal = Calendar.getInstance();
mDialog = new CustomerDatePickerDialog(getContext(), this,
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
cal.get(Calendar.DAY_OF_MONTH));
mDialog.show();
DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
}
代码说明:
通过源码可以看得到DatePicker内置三个NumberPicker控件,依次表示年、月、日,隐藏掉第一个即可。
补充
后续使用中发现标题栏也要改,通过查看DatePickerDialog源码,需要自定义并实现onDateChanged方法才可实现,如下代码:
class CustomerDatePickerDialog extends DatePickerDialog {
public CustomerDatePickerDialog(Context context,
OnDateSetListener callBack, int year, int monthOfYear,
int dayOfMonth) {
super(context, callBack, year, monthOfYear, dayOfMonth);
}
@Override
public void onDateChanged(DatePicker view, int year, int month, int day) {
super.onDateChanged(view, year, month, day);
mDialog.setTitle((month + 1) + "月" + day + "日");
}
}