2016-11-28 52 views
2

当我更改应用程序的语言环境时,每个TextView都会更改语言,但导航抽屉项目不会更改语言。更改导航抽屉项目的语言环境

我用

@Override 
public void onDrawerOpened(View drawerView) {  
    super.onDrawerOpened(drawerView); 
    invalidateOptionsMenu(); 
    drawerView.invalidate(); 
    supportInvalidateOptionsMenu(); 
} 

语言环境变化之后,仍然是语言的变化是没有得到体现在导航抽屉里。

但是,当我重新启动活动时,语言更改正在导航抽屉中反映出来。

如何在语言环境更改后立即更改导航抽屉项目中的语言?

+0

您可以分享您的完整代码以获得更多帮助吗? –

回答

3

您需要为您的应用程序重新加载资源才能更改语言。所有基于文本的资源在语言更改之前加载的资源都是以前的语言环境。现在,在语言更改后,您需要具有不同语言环境的资源。

因此,要做的是刷新整个活动,因为不仅需要不同的资源,而且不同语言的文本可能会有不同的长度和跨度。因此,需要全新的重新渲染,否则,文本将溢出其他UI元素,无需调整。

我会在这里发布完整的代码,刷新语言更改的活动,并且所有内容都会更改为不同的语言环境,包括导航抽屉项目。


代码

SRC/JAVA/COM/pabhinav/testapp/MainActivity.java

package com.pabhinav.testapp; 

import android.content.Intent; 
import android.content.res.Configuration; 
import android.content.res.Resources; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.NavigationView; 
import android.support.design.widget.Snackbar; 
import android.support.v4.view.GravityCompat; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
import android.util.DisplayMetrics; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 

import java.util.Locale; 

public class MainActivity extends AppCompatActivity 
     implements NavigationView.OnNavigationItemSelectedListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
     drawer.setDrawerListener(toggle); 
     toggle.syncState(); 

     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
     navigationView.setNavigationItemSelectedListener(this); 
    } 

    @Override 
    public void onBackPressed() { 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
      drawer.closeDrawer(GravityCompat.START); 
     } else { 
      super.onBackPressed(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    @SuppressWarnings("StatementWithEmptyBody") 
    @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 
     // Handle navigation view item clicks here. 
     int id = item.getItemId(); 

     if (id == R.id.nav_camera) { 
      // Handle the camera action 
     } else if (id == R.id.nav_gallery) { 

     } else if (id == R.id.nav_slideshow) { 

     } else if (id == R.id.nav_manage) { 

     } else if (id == R.id.nav_share) { 

     } else if (id == R.id.nav_send) { 

     } 

     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
     drawer.closeDrawer(GravityCompat.START); 
     return true; 
    } 

    // THIS PART CHANGES LANGUAGE : 
    public void changeLanguageClicked(View view) { 

     String locale = (getResources().getConfiguration().locale.toString().toLowerCase().equals(Locale.US.toString().toLowerCase())) ? "hi" : Locale.US.toString(); 
     Locale myLocale = new Locale(locale); 
     Resources res = getResources(); 
     DisplayMetrics dm = res.getDisplayMetrics(); 
     Configuration conf = res.getConfiguration(); 
     conf.locale = myLocale; 
     res.updateConfiguration(conf, dm); 
     Intent refresh = new Intent(this, MainActivity.class); 
     startActivity(refresh); 
     finish(); 
    } 
} 

SRC/RES /布局/ activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/drawer_layout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <include 
     layout="@layout/app_bar_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/nav_view" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:fitsSystemWindows="true" 
     app:headerLayout="@layout/nav_header_main" 
     app:menu="@menu/activity_main_drawer" /> 

</android.support.v4.widget.DrawerLayout> 

src/res/layout/app_bar_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.pabhinav.testapp.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    </android.support.design.widget.AppBarLayout> 

    <include layout="@layout/content_main" /> 

    <android.support.design.widget.FloatingActionButton 
     android:id="@+id/fab" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|end" 
     android:layout_margin="@dimen/fab_margin" 
     app:srcCompat="@android:drawable/ic_dialog_email" /> 

</android.support.design.widget.CoordinatorLayout> 

SRC/RES /布局/ content_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/content_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.pabhinav.testapp.MainActivity" 
    tools:showIn="@layout/app_bar_main"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text = "@string/change_language" 
     android:onClick = "changeLanguageClicked" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true" /> 

</RelativeLayout> 

SRC/RES /布局/ nav_main_header.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="@dimen/nav_header_height" 
    android:background="@drawable/side_nav_bar" 
    android:gravity="bottom" 
    android:orientation="vertical" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     app:srcCompat="@android:drawable/sym_def_app_icon" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="@dimen/nav_header_vertical_spacing" 
     android:text="@string/android_studio" 
     android:textAppearance="@style/TextAppearance.AppCompat.Body1" /> 

</LinearLayout> 

SRC/RES /菜单/ activity_main_drawer.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_camera" 
      android:icon="@drawable/ic_menu_camera" 
      android:title="@string/import_nav" /> 
     <item 
      android:id="@+id/nav_gallery" 
      android:icon="@drawable/ic_menu_gallery" 
      android:title="@string/gallery" /> 
     <item 
      android:id="@+id/nav_slideshow" 
      android:icon="@drawable/ic_menu_slideshow" 
      android:title="@string/slideshow" /> 
     <item 
      android:id="@+id/nav_manage" 
      android:icon="@drawable/ic_menu_manage" 
      android:title="@string/tools" /> 
    </group> 

    <item android:title="@string/communicate"> 
     <menu> 
      <item 
       android:id="@+id/nav_share" 
       android:icon="@drawable/ic_menu_share" 
       android:title="@string/share" /> 
      <item 
       android:id="@+id/nav_send" 
       android:icon="@drawable/ic_menu_send" 
       android:title="@string/send" /> 
     </menu> 
    </item> 

</menu> 

src/res/menu/main.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 
    <item 
     android:id="@+id/action_settings" 
     android:orderInCategory="100" 
     android:title="@string/action_settings" 
     app:showAsAction="never" /> 
</menu> 

SRC/RES /值/ strings.xml中

<resources> 
    <string name="app_name">TestApp</string> 

    <string name="navigation_drawer_open">Open navigation drawer</string> 
    <string name="navigation_drawer_close">Close navigation drawer</string> 

    <string name="action_settings">Settings</string> 
    <string name="import_nav">Import</string> 
    <string name="gallery">Gallery</string> 
    <string name="slideshow">Slideshow</string> 
    <string name="tools">Tools</string> 
    <string name="communicate">Communicate</string> 
    <string name="share">Share</string> 
    <string name="send">Send</string> 
    <string name="android_studio">Android Studio</string> 
    <string name="hello_world">Hello World!</string> 
    <string name="change_language">Change Language</string> 
</resources> 

SRC/RES /值喜/ strings.xml中

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <string name="app_name">परीक्षण ऐप</string> 
    <string name="navigation_drawer_open">खुले नेविगेशन दराज</string> 
    <string name="navigation_drawer_close">बंद नेविगेशन दराज</string> 
    <string name="action_settings">सेटिंग्स</string> 
    <string name="import_nav">आयात</string> 
    <string name="gallery">गेलरी</string> 
    <string name="slideshow">स्लाइड शो</string> 
    <string name="tools">उपकरण</string> 
    <string name="communicate">संवाद</string> 
    <string name="share">शेयर</string> 
    <string name="send">भेजना</string> 
    <string name="android_studio">एंड्रॉयड स्टूडियो</string> 
    <string name="hello_world">नमस्ते दुनिया</string> 
    <string name = "change_language">भाषा बदलो</string> 
</resources> 

SRC/AndroidManifest。XML

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.pabhinav.testapp"> 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme"> 
     <activity 
      android:name=".MainActivity" 
      android:configChanges="locale" 
      android:label="@string/app_name" 
      android:theme="@style/AppTheme.NoActionBar"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

寄托都上面我已经测试并能正常工作。

希望它能帮助!

+0

谢谢。这个答案值得赏赐。我明白了。 –