Kevin
04/04/2022, 8:43 AMactivity_main.xml
in form of <com.test_project.library.buttons.TestButton/>
, it did works perfectly, but i found out that i cannot override the text value because the android:text
doesn't appear from suggestion.
is this because the effect from inflating the views? or there is something that missing from the custom views class that i didn't declare it first so i cannot put text value in activity_main.xml
?Kevin
04/04/2022, 8:43 AMclass TestButton : RelativeLayout {
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context, attr: AttributeSet? = null) : super(context, attr) {
initView()
}
constructor(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int
) : super(context, attrs, defStyleAttr) {
initView()
}
private fun initView() {
val rootView = (context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater)
.inflate(R.layout.activity_button_default, this, true)
}
}
Christiano
04/04/2022, 10:07 AMandroid:text
will only be available, by default, if your custom view is a subclass of TextView
. In your case, you extend from the RelativeLayout
thus have no access to the xml attributes of the TextView.
1 way to fix this is by adding custom attributes to your custom view. See this post for more information.
https://medium.com/@Zielony/guide-to-android-custom-views-attributes-ab28de3e54b7Kevin
04/06/2022, 5:44 AM