Вы не можете изменять файлы ресурсов (включая макеты xml) во время выполнения.
Однако вы можете установить пользовательский LayoutInflater.Factory2 в Context
для изменения поведения синтаксического анализа XML.Пример ниже (обратите внимание, что я использую расширения Kotlin):
activity_style.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtTest"
android:layout_gravity="center"
android:text="Hello world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/btnTest"
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
sample.kt
import android.content.Context
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.support.v7.widget.AppCompatTextView
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import com.github.ppaszkiewicz.testvhpayload.R
import kotlinx.android.synthetic.main.activity_style.*
class StyleActivity : AppCompatActivity(){
override fun onCreate(savedInstanceState: Bundle?) {
// replace default factory with a wrapper - must be called before onCreate
layoutInflater.factory = MyTextViewFactory(delegate)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_style)
Log.d("TEST","TextView is ${txtTest::class.simpleName}")
Log.d("TEST","Button is ${btnTest::class.simpleName}")
}
}
class MyTextViewFactory(val appCompatDelegate: AppCompatDelegate) : LayoutInflater.Factory2{
override fun onCreateView(parent: View?, name: String, context: Context, attrs: AttributeSet): View? {
if(name == "TextView")
return MyTextView(context, attrs) // custom behaviour for TextViews
return appCompatDelegate.createView(parent, name, context, attrs) // default appcompat views
}
override fun onCreateView(name: String, context: Context, attrs: AttributeSet): View? {
return onCreateView(null, name, context, attrs)
}
}
class MyTextView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatTextView(context, attrs, defStyleAttr)
После запуска в логах должно отображаться
TextView is MyTextView
Button is AppCompatButton