के लिए Attrs तक कैसे पहुंचे I Kotlin में एक कस्टम दृश्य बनाएं, और इसके गुण संसाधनों तक पहुंच बनाना चाहूंगा।कोटलिन: कस्टमव्यू
नीचे मेरी कोड
class CustomCardView : FrameLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
init {
LayoutInflater.from(context).inflate(R.layout.view_custom_card, this, true)
if (attrs != null) {
val a = context.obtainStyledAttributes(attrs, R.styleable.custom_card_view)
if (a.hasValue(R.styleable.custom_card_view_command)) {
var myString = a.getString(R.styleable.custom_card_view_command)
}
}
}
}
ध्यान दें कि यह init समारोह में attrs
में त्रुटि नहीं है। मैं सोच रहा हूं कि attrs
तक कैसे पहुंचे?
धन्यवाद! मैं सोच रहा था कि डिफ़ॉल्ट init() कन्स्ट्रक्टर के माध्यम से कैसे पहुंचे। – Elye
आप ** का उपयोग भी कर सकते हैं? {} ** कथन, attrs? .let {initAttrs (संदर्भ, यह)} – Yvgen