//custom_item_menu.xml - 커스텀 뷰
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="25dp"
android:layout_marginEnd="25dp">
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvMenu"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold"
tools:text="타코야키 8알(기본맛, 매운맛)" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="end"
android:textColor="@color/textGrey"
android:textSize="15sp"
tools:text="3,000원" />
</TableRow>
</androidx.appcompat.widget.LinearLayoutCompat>
</layout>
//values>attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MenuCommon">
<attr name="tvMenu" format="string" /> //reference 는 strings.xml 에 있는 값 참조할 수 있음.
<attr name="tvPrice" format="string" />
</declare-styleable>
</resources>
class MenuCommon : LinearLayoutCompat {
private lateinit var binding: ItemMenuBinding
private lateinit var tvMenu: AppCompatTextView
private lateinit var tvPrice: AppCompatTextView
constructor(context: Context) : super(context) {
initView()
}
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet) {
initView()
getAttrs(attributeSet)
}
constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int) : super(
context,
attributeSet,
defStyleAttr
) {
initView()
getAttrs(attributeSet, defStyleAttr)
}
private fun initView() {
val inflater = LayoutInflater.from(context)
binding = ItemMenuBinding.inflate(inflater, this, false)
tvMenu = binding.tvMenu
tvPrice = binding.tvPrice
addView(binding.root)
}
private fun getAttrs(attrs: AttributeSet) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuCommon)
setTypeArray(typedArray)
}
private fun getAttrs(attrs: AttributeSet, defStyleAttr: Int) {
val typedArray = context.obtainStyledAttributes(attrs, R.styleable.MenuCommon, defStyleAttr, 0)
setTypeArray(typedArray)
}
private fun setTypeArray(typedArray: TypedArray) {
val menu = typedArray.getString(R.styleable.MenuCommon_tvMenu)
val price = typedArray.getString(R.styleable.MenuCommon_tvPrice)
this.tvMenu.text = menu ?: ""
this.tvPrice.text = price ?: ""
typedArray.recycle()
}
fun setMenu(menu: String?) {
this.tvMenu.text = menu
}
fun setContent(price: String?) {
this.tvPrice.text = price
}
}
private fun initMenu(){
val tr = MenuCommon(requireContext())
tr.setMenu("닭꼬치")
tr.setPrice("3,000원")
binding.shopBottom.tl.addView(tr)
}
https://gun0912.tistory.com/38