android 自定义微件未显示在布局中

6za6bjd0  于 2023-04-28  发布在  Android
关注(0)|答案(1)|浏览(164)

我创建了一个自定义视图

class TravelContactDetailsWidget : ConstraintLayout {

    private lateinit var binding : TravelContactDetailsWidgetBinding

    constructor(context: Context) : super(context) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView()
    }

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {
        initView()
    }

    private fun initView() {
        val inflater =
            context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        binding = TravelContactDetailsWidgetBinding.inflate(inflater)
    }

}

布局文件如下

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.appcompat.widget.LinearLayoutCompat android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvContactDetails"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="sans-serif-medium"
        android:text="@string/label_countries"
        android:textColor="@color/white_label_primary_color"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.appcompat.widget.LinearLayoutCompat>
</layout>

但是,在将小部件添加到另一个布局后运行应用程序时,它不会显示
我已经添加到下面的片段布局和它的空

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        app:layout_constraintBottom_toBottomOf="@+id/tvUseCardAbroad"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.2"
        app:layout_constraintStart_toEndOf="@+id/tvUseCardAbroad"
        app:layout_constraintTop_toTopOf="@+id/tvUseCardAbroad"
        android:theme="@style/SBGSwitch" />

    <TextView
        android:id="@+id/tvUseCardAbroad"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="8dp"
        android:text="TextView"
        app:layout_constraintEnd_toStartOf="@+id/switch1"
        app:layout_constraintHorizontal_bias="0.8"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tvUseCardAbroadDisclaimerText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:layout_marginEnd="16dp"
        android:text="@string/label_activate_card_note"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="@+id/tvUseCardAbroad"
        app:layout_constraintTop_toBottomOf="@+id/tvUseCardAbroad"
        tools:visibility="invisible" />

    <com.myapp.mobile.android.travel.TravelDateRangePickerWidget
        android:id="@+id/travelDateRangePickerWidget"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/tvUseCardAbroadDisclaimerText"
        android:visibility="visible"/>

    <com.myapp.mobile.android.travel.TravelCountrySelectorWidget
        android:id="@+id/travelCountrySelectorWidget"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:layout_marginTop="8dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/travelDateRangePickerWidget"/>

    <com.myapp.mobile.android.travel.TravelContactDetailsWidget
        android:layout_width="match_parent"
        android:layout_height="120dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/travelCountrySelectorWidget" />
</androidx.constraintlayout.widget.ConstraintLayout>

我是不是漏掉了什么?

ecfdbz9o

ecfdbz9o1#

我是不是漏掉了什么?
您正在扩展绑定,但实际上并没有对它做任何操作,因此自定义视图仍然是空的。

// Inflates binding and holds a reference but not doing anything with it
binding = TravelContactDetailsWidgetBinding.inflate(inflater)

要解决这个问题,请使用inflate版本,它接受视图将 * 膨胀为 *:

binding = TravelContactDetailsWidgetBinding.inflate(inflater, this, true)

相关问题