我正在开发一个android应用程序,它有一个设置选项来改变语言。它运行良好,但谷歌登录按钮不会改变语言,直到我关闭应用程序,并重新打开它。
这是onCreate方法的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
String language = "en";
if (Locale.getDefault().getLanguage().equals("es")) {
language = "es";
}
String languagePref = prefs.getString(activity.getResources().getString(R.string.settings_language_key), language);
Locale locale = new Locale(languagePref);
Locale.setDefault(locale);
Configuration config = new Configuration();
config.locale = locale;
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
setContentView(start_view);
SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button);
// Set the dimensions of the sign-in button.
if ((getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) == Configuration.SCREENLAYOUT_SIZE_XLARGE) {
signInButton.setSize(SignInButton.SIZE_WIDE);
} else {
signInButton.setSize(SignInButton.SIZE_STANDARD);
}这是布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".StartActivity"
android:id="@+id/drawer_layout"
style="@style/StartViewDrawerContainer">
<RelativeLayout
style="@style/StartViewParentVertical">
<!-- Some GUI elements -->
<LinearLayout
style="@style/StartViewMainContainer">
<!-- More GUI elements -->
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_button"
style="@style/StartViewSignInButton"/>
</LinearLayout>
</RelativeLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
style="@style/StartViewNavigationView"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>这段代码适用于每一个字符串文字,但对于google按钮则不然。
是否有任何方法以编程方式在文本中本地化符号?
发布于 2017-10-19 18:40:53
最后,我已经解决了为每种语言定义文字并使用说明编程设置它的问题。
TextView signInText = (TextView) signInButton.getChildAt(0);
signInText.setText(getString(R.string.sign_in));发布于 2018-02-12 05:04:11
问题与解决方案:
通过使用像signInButton.getChildAt(0);这样的解决方法,问题是按钮的底层实现可能随时改变,从而导致代码中断。
清洁解决方案:
对于一个干净的解决方案,谷歌建议创建自定义按钮。为了防止每次重新做这些工作,我创建了一个可重用的自定义按钮,作为一个轻量级的4KB库,您可以在这个库中选择在button本身上安装android:text。这将根据语言集从string.xml文件中获取正确的本地化。
https://github.com/shobhitpuri/custom-google-signin-button
希望能帮上忙。
https://stackoverflow.com/questions/46088683
复制相似问题