2017-09-07 37 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}":在藍色主題和白色主題之間切換按鈕。該庫處理文本顏色和背景顏色的變化。它還處理按鈕按下或按鈕單擊時顏色的變化。

來源

希望它能幫助。