Android 如何在kotlin的xml标记中自动添加相同的xml代码?
我想在我的xml文件中添加以下xml代码,TextView自动标记,我该怎么做 我想在打开标签后添加的代码是:Android 如何在kotlin的xml标记中自动添加相同的xml代码?,android,xml,kotlin,xml-parsing,custom-controls,Android,Xml,Kotlin,Xml Parsing,Custom Controls,我想在我的xml文件中添加以下xml代码,TextView自动标记,我该怎么做 我想在打开标签后添加的代码是: com.mycompany.projectname. 我想插入上述代码的标记是: <TextView android:id="@+id/textView16" android:layout_width="wrap_content" /> 我的预期输出如下所示 <com.mycompany.projectname.TextView android
com.mycompany.projectname.
我想插入上述代码的标记是:
<TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
/>
我的预期输出如下所示
<com.mycompany.projectname.TextView
android:id="@+id/textView16"
android:layout_width="wrap_content"
/>
android studio不会以XML格式自动获取/添加自定义小部件。 您必须使用包手动指定类名 在预定义窗口小部件中,如果要添加某些属性,请按 Ctrl+Space用于windows mac的命令+空间
您不能在运行时修改资源文件(包括xml布局) 但是,您可以在
上下文中安装自定义项来修改XML解析行为。下面的示例(请注意,我使用的是Kotlin扩展):
活动\u 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>
运行日志后应显示
TextView is MyTextView
Button is AppCompatButton
我不想说这件事。我希望你已经理解了我的问题。@UsmanHassanChattha你提到了Kotlin并使用了xml解析标记,所以我假设你指的是运行时,否则,您要查找的是项目源文件中的粗略查找+替换。如何在运行时解析xml?@UsmanHassanChattha运行时布局xml解析已由LayoutInflater
class完成,我的回答明确显示了如何在AppCompatActivity
中修改它的行为,使TextView
标记在XML中膨胀为自定义类。
TextView is MyTextView
Button is AppCompatButton