Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/197.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 当添加许多模块时,区域设置中的更改确实起作用_Android_Kotlin_Locale_Android Context_Crowdin - Fatal编程技术网

Android 当添加许多模块时,区域设置中的更改确实起作用

Android 当添加许多模块时,区域设置中的更改确实起作用,android,kotlin,locale,android-context,crowdin,Android,Kotlin,Locale,Android Context,Crowdin,当有许多模块时,区域设置确实起作用 背景: 我们使用Crowdin(这个库在上下文之上应用一个包装器) 当只有一个模块时,该应用程序可以完美运行 使用Appcompat:1.2 当更改区域设置起作用时 但是,当我在app中添加新模块时,更改区域设置确实有效实施项目(“:newmodule”) 当is为单模块时: BaseContext=crowdinTextWrapper 当是多模块时: BaseContext=ContextThemeWrapper 活动扩展基本活动 cl

当有许多模块时,区域设置确实起作用

背景:

  • 我们使用Crowdin(这个库在上下文之上应用一个包装器)

  • 当只有一个模块时,该应用程序可以完美运行

  • 使用Appcompat:1.2

  • 当更改区域设置起作用时

但是,当我在
app
中添加新模块时,更改区域设置确实有效<代码>实施项目(“:newmodule”)

当is为单模块时

  • BaseContext
    =
    crowdinTextWrapper
当是多模块时:

  • BaseContext
    =
    ContextThemeWrapper

活动
扩展
基本活动

class MainActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        Crowdin.forceUpdate(context = this)
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}
解释 该问题与
Appcompat
有关。根据
AppCompat
的版本,有两种不同的修复程序。由于您使用的是
1.2.0
,因此必须实现该功能

appcompatedelegateimpl
最终删除了区域设置,因为实际上是一个包装。见实施@。还有线,还有

解决这一问题的办法是在基本活动中超越
getDelegate
,如下所示:

private var baseContextWrappingDelegate: AppCompatDelegate? = null

override fun getDelegate() = baseContextWrappingDelegate ?: BaseContextWrappingDelegate(super.getDelegate()).apply {
    baseContextWrappingDelegate = this
}
您还需要以下类(请参阅:)

包androidx.appcompat.app
导入android.content.Context
导入android.content.res.Configuration
导入android.os.Bundle
导入android.util.AttributeSet
导入android.view.MenuInflater
导入android.view.view
导入android.view.ViewGroup
导入androidx.appcompat.view.ActionMode
导入androidx.appcompat.widget.Toolbar
类BaseContextWrappingDelegate(私有val superDelegate:AppCompatDelegate):AppCompatDelegate(){
覆盖getSupportActionBar()=superDelegate.supportActionBar
覆盖有趣的setSupportActionBar(工具栏:工具栏?)=superDelegate.setSupportActionBar(工具栏)
重写getMenuInflater():MenuInflater?=superDelegate.MenuInflater
重写创建时的乐趣(savedInstanceState:Bundle?){
superDelegate.onCreate(savedInstanceState)
removeActivityDelegate(超级代理)
addActiveDelegate(此)
}
重写onPostCreate(savedInstanceState:Bundle?=superDelegate.onPostCreate(savedInstanceState)
override-fun-onConfigurationChanged(newConfig:Configuration?)=superDelegate.onConfigurationChanged(newConfig)
override fun onStart()=superDelegate.onStart()
override fun onStop()=superDelegate.onStop()
重写onPostResume()=超级委托。onPostResume()
override fun setTheme(themeResId:Int)=superDelegate.setTheme(themeResId)
重写findViewById(id:Int)=superDelegate.findViewById(id)
覆盖乐趣setContentView(v:View?)=superDelegate.setContentView(v)
覆盖乐趣setContentView(resId:Int)=superDelegate.setContentView(resId)
覆盖有趣的setContentView(v:View?,lp:ViewGroup.LayoutParams?)=superDelegate.setContentView(v,lp)
覆盖有趣的addContentView(v:View?,lp:ViewGroup.LayoutParams?)=superDelegate.addContentView(v,lp)
override fun attachBaseContext2(上下文:context)=wrap(superDelegate.attachBaseContext2(super.attachBaseContext2(上下文)))
重写有趣的setTitle(标题:CharSequence?)=superDelegate.setTitle(标题)
override fun invalidateOptionsMenu()=超级委托。invalidateOptionsMenu()
重写onDestroy(){
superDelegate.onDestroy()
removeActivityDelegate(此)
}
重写GetDroperToggleLegate()=superDelegate.DroperTogglegate
override fun requestWindowFeature(featureId:Int)=superDelegate.requestWindowFeature(featureId)
覆盖hasWindowFeature(featureId:Int)=超级委托。hasWindowFeature(featureId)
覆盖有趣的startSupportActionMode(回调:ActionMode.callback)=superDelegate.startSupportActionMode(回调)
重写installViewFactory()=超级委托。installViewFactory()
重写有趣的createView(父:视图?,名称:字符串?,上下文:上下文,属性集):视图?=superDelegate.createView(父,名称,上下文,属性)
override fun SETHANDLENATIVEATIONMODESENABLED(已启用:布尔值){
superDelegate.isHandleNativeActionModesEnabled=已启用
}
重写isHandleNativeActionModesEnabled()=superDelegate.isHandleNativeActionModesEnabled
覆盖onSaveInstanceState(超出状态:Bundle?)=superDelegate.onSaveInstanceState(超出状态)
override fun applyDayNight()=超级委托。applyDayNight()
覆盖fun setLocalNightMode(模式:Int){
superDelegate.localNightMode=模式
}
重写getLocalNightMode()=superDelegate.localNightMode
私人趣味包装(上下文:上下文):上下文{
//将包装实现放在这里
}
}
工具书类

  • 请添加有关该问题的额外信息。您的代码体很好,但我认为您关于单模块和多模块的陈述是不够的。如果您添加一些额外的信息,这将帮助开发人员更好地帮助您。我解决了这个问题及其与Appcompat:1.2.0的关系
    class Localization(base: Context) : ContextThemeWrapper(base, R.style.AppTheme) {
        companion object {
    
            fun wrap(context: Context, language: String = "es", country: String = "MX"): ContextThemeWrapper {
                var ctx = context
                val config = context.resources.configuration
    
                if (language != "") {
                    val locale = Locale(language, country)
                    Locale.setDefault(locale)
                    config.setLocale(locale)
                    // Used setLayoutDirection for RTL and LTR
                    config.setLayoutDirection(locale)
                    ctx = context.createConfigurationContext(config)
    
                }
    
                return Localization(ctx)
            }
        }
    }
    
    try {
                    ContextThemeWrapperCompatApi17Impl.applyOverrideConfiguration(
                            (android.view.ContextThemeWrapper) baseContext, config);
                    return baseContext;
                } catch (IllegalStateException e) {
                    if (DEBUG) {
                        Log.d(TAG, "Failed to apply configuration to base context", e);
                    }
                }
    
    private var baseContextWrappingDelegate: AppCompatDelegate? = null
    
    override fun getDelegate() = baseContextWrappingDelegate ?: BaseContextWrappingDelegate(super.getDelegate()).apply {
        baseContextWrappingDelegate = this
    }
    
    package androidx.appcompat.app
    
    import android.content.Context
    import android.content.res.Configuration
    import android.os.Bundle
    import android.util.AttributeSet
    import android.view.MenuInflater
    import android.view.View
    import android.view.ViewGroup
    import androidx.appcompat.view.ActionMode
    import androidx.appcompat.widget.Toolbar
    
    class BaseContextWrappingDelegate(private val superDelegate: AppCompatDelegate) : AppCompatDelegate() {
    
        override fun getSupportActionBar() = superDelegate.supportActionBar
    
        override fun setSupportActionBar(toolbar: Toolbar?) = superDelegate.setSupportActionBar(toolbar)
    
        override fun getMenuInflater(): MenuInflater? = superDelegate.menuInflater
    
        override fun onCreate(savedInstanceState: Bundle?) {
            superDelegate.onCreate(savedInstanceState)
            removeActivityDelegate(superDelegate)
            addActiveDelegate(this)
        }
    
        override fun onPostCreate(savedInstanceState: Bundle?) = superDelegate.onPostCreate(savedInstanceState)
    
        override fun onConfigurationChanged(newConfig: Configuration?) = superDelegate.onConfigurationChanged(newConfig)
    
        override fun onStart() = superDelegate.onStart()
    
        override fun onStop() = superDelegate.onStop()
    
        override fun onPostResume() = superDelegate.onPostResume()
    
        override fun setTheme(themeResId: Int) = superDelegate.setTheme(themeResId)
    
        override fun <T : View?> findViewById(id: Int) = superDelegate.findViewById<T>(id)
    
        override fun setContentView(v: View?) = superDelegate.setContentView(v)
    
        override fun setContentView(resId: Int) = superDelegate.setContentView(resId)
    
        override fun setContentView(v: View?, lp: ViewGroup.LayoutParams?) = superDelegate.setContentView(v, lp)
    
        override fun addContentView(v: View?, lp: ViewGroup.LayoutParams?) = superDelegate.addContentView(v, lp)
    
        override fun attachBaseContext2(context: Context) = wrap(superDelegate.attachBaseContext2(super.attachBaseContext2(context)))
    
        override fun setTitle(title: CharSequence?) = superDelegate.setTitle(title)
    
        override fun invalidateOptionsMenu() = superDelegate.invalidateOptionsMenu()
    
        override fun onDestroy() {
            superDelegate.onDestroy()
            removeActivityDelegate(this)
        }
    
        override fun getDrawerToggleDelegate() = superDelegate.drawerToggleDelegate
    
        override fun requestWindowFeature(featureId: Int) = superDelegate.requestWindowFeature(featureId)
    
        override fun hasWindowFeature(featureId: Int) = superDelegate.hasWindowFeature(featureId)
    
        override fun startSupportActionMode(callback: ActionMode.Callback) = superDelegate.startSupportActionMode(callback)
    
        override fun installViewFactory() = superDelegate.installViewFactory()
    
        override fun createView(parent: View?, name: String?, context: Context, attrs: AttributeSet): View? = superDelegate.createView(parent, name, context, attrs)
    
        override fun setHandleNativeActionModesEnabled(enabled: Boolean) {
            superDelegate.isHandleNativeActionModesEnabled = enabled
        }
    
        override fun isHandleNativeActionModesEnabled() = superDelegate.isHandleNativeActionModesEnabled
    
        override fun onSaveInstanceState(outState: Bundle?) = superDelegate.onSaveInstanceState(outState)
    
        override fun applyDayNight() = superDelegate.applyDayNight()
    
        override fun setLocalNightMode(mode: Int) {
            superDelegate.localNightMode = mode
        }
    
        override fun getLocalNightMode() = superDelegate.localNightMode
    
        private fun wrap(context: Context): Context {
            //Put wrapping implementation here
        }
    }