Troubles with activity title language

前端 未结 4 1066
一个人的身影
一个人的身影 2021-02-18 20:26

I have two language in my app. values/strings.xml and values-ru/strings.xml When I programmatically change language, all strings translating, b

4条回答
  •  情书的邮戳
    2021-02-18 20:52

    Finally got a solution:


    Get resouce Id of the activity's title from PackageManger, and set the AcionBar's title to the resource id, this time because the Locale is already changed android knows which activity title to pick.

    try{
    
        ActivityInfo activityInfo = getPackageManager().getActivityInfo(getComponentName(), 
            PackageManager.GET_META_DATA);
    
        ActionBar actionBar = getActionBar();
    
        if(actionBar != null)
            actionBar.setTitle(activityInfo.labelRes);
    
    }catch (PackageManager.NameNotFoundException ex){
    
        Log.e(this.getClass().getSimpleName(), 
            "Error while getting activity info. " + ex.getMessage(), ex);
    
    }
    

提交回复
热议问题