2017-09-07 38 views
1

我正在开发一个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> 

此代码为每一个字符串文字而不是谷歌的按钮的伟大工程。

有没有什么办法来以编程方式本地化登录文本?

+0

谷歌标志按钮它不是简单的按钮。你可以使用后添加谷歌GSM库如果你想改变谷歌签名按钮的语言比添加XML简单的按钮和设计像谷歌按钮比你可以改变谷歌登录按钮语言 –

+0

我不明白你是什么意思,当你说加入谷歌GSM图书馆。你能给我更多的细节吗?谢谢! –

+0

我想你想在你的应用中使用谷歌身份验证?比你想在你的gradle中使用这个库。编译'com.google.android.gms:play-services-auth:9.2.1' –

回答

0

最后,我已经解决了定义文字的每一种语言,并与说明书

TextView signInText = (TextView) signInButton.getChildAt(0); 
signInText.setText(getString(R.string.sign_in)); 
+0

你可以以编程方式触发UI刷新当地的变化。 Android广播您可以在应用程序中捕获的意图。请参阅'ACTION_LOCALE_CHANGED' – Jon

0

问题与解决方法编程设置它的问题:

使用的解决方法就像signInButton.getChildAt(0);,发行是该按钮的底层实现可能会改变任何可能导致代码中断的时间。

干净的解决方案:

对于一个干净的解决方案,谷歌建议creating a custom button。为了防止每次重做,我创建了一个可重复使用的自定义按钮,作为一个轻型4KB库,您可以在Button上选择android:text。这将根据语言设置从string.xml文件中获取正确的本地化。

  • 步骤1:以下内容添加到您的应用程序模块级的build.gradle文件:

    dependencies { 
        compile 'com.shobhitpuri.custombuttons:google-signin:1.0.0' 
    } 
    
  • 步骤2:在你的XML布局,有以下几点:

    <com.shobhitpuri.custombuttons.GoogleSignInButton 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerInParent="true" 
        android:text="@string/google_sign_up" 
        app:isDarkTheme="true" /> 
    

使用

  • android:text="{string}":像往常一样设置按钮上的文字。

  • app:isDarkTheme="{Boolean}":在蓝色主题和白色主题之间切换按钮。该库处理文本颜色和背景颜色的变化。它还处理按钮按下或按钮单击时颜色的变化。

来源

希望它能帮助。