【问题标题】:how to add same xml code Automatically in xml tags in kotlin?如何在kotlin的xml标签中自动添加相同的xml代码?
【发布时间】:2019-03-26 09:49:30
【问题描述】:

我想在我的 xml 文件中自动添加以下 xml 代码,TextView 标签,我该怎么做?

我想在打开标签后添加的代码是:

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 xml kotlin xml-parsing custom-controls


    【解决方案1】:

    自定义小部件 android studio 不会在 XML 中自动获取/添加。 你必须手动给你的类名加上包。

    如果你想在预定义的小部件中添加一些属性,请按

    Ctrl + Space 用于窗口

    Mac 的 Command+ 空格

    【讨论】:

      【解决方案2】:

      您不能在运行时修改资源文件(包括 xml 布局)。

      但是,您可以在 Context 中安装自定义 LayoutInflater.Factory2 以修改 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
      

      【讨论】:

      • 我不要这个东西。
      • 希望你能理解我的问题。
      • @UsmanHassanChattha 你提到了 Kotlin 并使用了 xml-parsing 标签,所以我假设你的意思是运行时,否则你正在寻找的是在项目源文件中粗略的查找+替换。
      • 如何在运行时解析 xml?
      • @UsmanHassanChattha 运行时布局 XML 解析已由 LayoutInflater 类完成,我的回答明确显示了如何修改其在 AppCompatActivity 中的行为,以使 XML 中的 TextView 标记膨胀为自定义类。
      猜你喜欢
      • 1970-01-01
      • 2021-11-28
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多