我有这个奇怪的行为时,创建一个自定义视图和修改文本和可见性,视图看起来像它的渲染两次。
在这里,我将更改标题和描述文本,并显示按钮版本详细信息
这是自定义视图
class KitChoiceItemView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0): ConstraintLayout(context, attrs, defStyle) {
private var binding: KitChooiceItemViewBinding
private var isActive = false
init {
LayoutInflater.from(context).inflate(R.layout.kit_chooice_item_view, this, true)
binding = KitChooiceItemViewBinding.inflate(LayoutInflater.from(context), this, true)
}
fun setupToHome() {
binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_home))
binding.titleText.text = "Entrega a domicilio"
binding.descriptionText.text = "Agrega una dirección para que podamos hacerte entrega de tu Kit a esa ubicación"
binding.priceText.visibility = View.VISIBLE
binding.detailsButton.visibility = View.GONE
binding.selectButton.setOnClickListener {
if (isActive) {
isActive = false
binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.gray_border)
} else {
isActive = true
binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.kit_item_selected)
}
}
}
fun setupToPlace() {
binding.iconCircle.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.ic_insc_location))
binding.titleText.text = "Recoger kit en la expo"
binding.descriptionText.text = "Al seleccionar esta opción, omite el paso de entrega a domicilio y continuas para realizar el pago."
binding.priceText.visibility = View.GONE
binding.detailsButton.visibility = View.VISIBLE
binding.selectButton.setOnClickListener {
if (isActive) {
isActive = false
binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.gray_border)
} else {
isActive = true
binding.cardView.background = ContextCompat.getDrawable(context, R.drawable.kit_item_selected)
}
}
}}
这就是我在框架布局中实现自定义视图的方式
<com.asdeporte.asdeportev2.ui.reusableview.inscription.KitChoiceItemView
android:id="@+id/address_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/
<com.asdeporte.asdeportev2.ui.reusableview.inscription.KitChoiceItemView
android:id="@+id/delivery_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"/>
设置自定义状态
binding.addressView.setupToHome()
binding.deliveryView.setupToPlace()
1条答案
按热度按时间zdwk9cvp1#
我放大了布局两次,不得不删除R布局