我编写了一个自定义视图组,并为这个自定义视图组的子级定义了两个属性以供使用,如 ConstraintLayout 的layout_constraintLeft_toLeftOf
,我的属性是 stayLeft
。
当我使用stayLeft
属性时,项目可以正常运行。但是 IDE 总是提示我:为标记 TextView 找到意外的命名空间前缀"app"更少...
我的视图组
class MyViewGroup @JvmOverloads constructor(
context: Context,
attributeSet: AttributeSet? = null,
defStyleAttr: Int = 0
) : ViewGroup(context, attributeSet, defStyleAttr) {
override fun generateLayoutParams(attrs: AttributeSet?): MyLayoutParams {
return MyLayoutParams(context, attrs)
}
override fun checkLayoutParams(p: LayoutParams?): Boolean {
return p is MyLayoutParams
}
override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
// TODO("not implemented")
}
class MyLayoutParams(context: Context, attrs:AttributeSet?) : ViewGroup.MarginLayoutParams(context, attrs) {
private var stayLeft : Int = 0
private var stayRight : Int = 0
init {
val a = context.obtainStyledAttributes(
attrs,
R.styleable.MyViewGroup_Layout
)
stayLeft = a.getResourceId(R.styleable.MyViewGroup_Layout_stayLeft, 0)
stayRight = a.getResourceId(R.styleable.MyViewGroup_Layout_stayRight, 0)
a.recycle()
}
}
}
吸引人.xml
<declare-styleable name="MyViewGroup_Layout">
<attr name="stayLeft" format="reference" />
<attr name="stayRight" format="reference" />
</declare-styleable>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.aprz.myapplication.MyViewGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
app:stayRight="@id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.aprz.myapplication.MyViewGroup>
</androidx.constraintlayout.widget.ConstraintLayout>
此行上的错误提示:
app:stayRight="@id/tv1"
这看起来 IDE 无法识别我的自定义属性。我应该怎么做才能消除错误提示???为什么约束布局没有此错误???我不想抑制此 lint 错误。
在 xml 中布局的开始标记中添加以下内容:
xmlns:app="http://schemas.android.com/apk/res-auto"
例如,如果你的应用包是 com.li.myapp ,您的布局现在如下所示:
<com.aprz.myapplication.MyViewGroup
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
app:stayRight="@id/tv1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.aprz.myapplication.MyViewGroup>
至于为什么它没有在 ConstraintLayout 中显示您的错误,您没有在此处发布完整的代码,所以我只能进行猜测。可能是在约束布局中,此属性具有除"app"以外的其他名称。
编辑:我刚刚注意到您的样式名称与您的类不同。 尝试将可样式名称重命名为与类名称相同的名称,如下所示:
<declare-styleable name="MyViewGroup">
<attr name="stayLeft" format="reference" />
<attr name="stayRight" format="reference" />
</declare-styleable>