我正在使用 TextClock
来显示现在是哪一天和哪一天。
是否可以更改 TextClock 的默认语言(英语)?
<TextClock
android:id="@+id/date"
style="@style/DateStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:format12Hour="EEEE\n MMMM d"
android:format24Hour="EEEEd\n MMMM"/>
显示:
最佳答案
您可以通过编写自定义 TextClock
并在其中设置 Locale
来实现。试试这个:
public class MyTextClock extends android.widget.TextClock {
public MyTextClock(Context context) {
super(context);
setLocaleDateFormat();
}
public MyTextClock(Context context, AttributeSet attrs) {
super(context, attrs);
setLocaleDateFormat();
}
public MyTextClock(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setLocaleDateFormat();
}
private void setLocaleDateFormat() {
// You can change language from here
Locale currentLocale = new Locale("en");
Calendar cal = GregorianCalendar.getInstance(TimeZone.getDefault(), currentLocale);
String dayName = cal.getDisplayName(cal.DAY_OF_WEEK, Calendar.LONG, currentLocale);
String monthName = cal.getDisplayName(cal.MONTH, Calendar.LONG, currentLocale);
this.setFormat12Hour("'" + dayName + "'\n'" + monthName + "' dd");
this.setFormat24Hour("'" + dayName + "'\n'" + monthName + "' dd");
}
}
将这个自定义 TextClock
实现到您的布局中,如下所示:
<com.your.package.MyTextClock
android:id="@+id/date"
style="@style/DateStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
给你。祝你好运。
关于android - 是否可以更改 xml 中 TextClock View 显示的语言,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44520930/