0

动态添加自定义视图时遇到问题。

以下是我当前的代码。

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SkillItemView">
        <attr name="skill" format="string" />
        <attr name="proficiency" format="integer" />
    </declare-styleable>
</resources>

Skill_item.view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/progress_item_incomplete"
    android:paddingTop="@dimen/list_row_margin_default"
    android:paddingBottom="@dimen/list_row_margin_default"
    android:paddingLeft="@dimen/text_padding"
    android:paddingRight="@dimen/text_padding"
    android:layout_margin="@dimen/list_row_margin_default">

    <TextView
        android:id="@+id/text_skill"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="@color/colorBlack_2"
        android:textStyle="italic"
        android:text="Art Direction" />
</LinearLayout>

技能项目视图.kt

class SkillItemView (context: Context, attrs: AttributeSet): LinearLayout(context, attrs) {

    init {
        inflate(context, R.layout.skill_item_view, this)

        val textSkill: TextView = findViewById(R.id.text_skill)

        val attributes = context.obtainStyledAttributes(attrs, R.styleable.SkillItemView)
        textSkill.text = attributes.getString(R.styleable.SkillItemView_skill)
        attributes.recycle()

    }
}

我将在适配器中动态添加此视图

for (skill in profileList[position].getSkills()) {
    var skillView = SkillItemView(context, ???)
    parentView.addView(skillView)
}

SkillItemView 构造函数有 2 个参数。上下文和属性集。(参见上述代码中的???)

我必须为 AttributeSet 写什么?

4

1 回答 1

1

您必须将attrs属性声明为 nullable AttributeSet?SkillItemView在这种情况下,您可以在适配器中实例化:

class SkillItemView(context: Context, attrs: AttributeSet? = null) : LinearLayout(context, attrs) {
    init {
        inflate(context, R.layout.skill_item_view, this)

        val textSkill: TextView = findViewById(R.id.text_skill)

        if (attrs != null) {
            val attributes = context.obtainStyledAttributes(attrs, R.styleable.SkillItemView)
            textSkill.text = attributes.getString(R.styleable.SkillItemView_skill)
            attributes.recycle()
        }
    }
}

...

for (skill in profileList[position].getSkills()) {
    var skillView = SkillItemView(context)
    parentView.addView(skillView)
}

如果要在SkillItemView动态创建时传递参数,可以创建自定义构造函数:

class SkillItemView : LinearLayout {
    constructor(context: Context, attrs: AttributeSet? = null) : super(context, attrs) {
        inflate(context, R.layout.skill_item_view, this)
        val textSkill: TextView = findViewById(R.id.text_skill)

        if (attrs != null) {
            val attributes = context.obtainStyledAttributes(attrs, R.styleable.SkillItemView)
            textSkill.text = attributes.getString(R.styleable.SkillItemView_skill)
            attributes.recycle()
        }
    }

    constructor(context: Context, skill:String? = null, proficiency: Int? = null) : super(context) {
        inflate(context, R.layout.skill_item_view, this)
        if (skill != null) {
            val textSkill: TextView = findViewById(R.id.text_skill)
            textSkill.text = skill
        }
    }
}
...
for (skill in profileList[position].getSkills()) {
    var skillView = SkillItemView(context, "test skill")
    parentView.addView(skillView)
}
于 2019-10-06T08:16:20.887 回答