【问题标题】:Switch to Display ConstrainedLayout not working切换到显示 ConstrainedLayout 不起作用
【发布时间】:2019-07-19 07:05:11
【问题描述】:

我正在尝试构建一个简单的 Android 开关 (availabilitiesSwitch) 以显示约束布局的内容。

开关如下所示:

<Switch
            android:id="@+id/availabilitiesSwitch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginTop="24dp"
            android:layout_marginEnd="16dp"
            android:showText="false"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

片段中的代码是这样的:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        (activity as BaseActivity).run {

            availabilitiesSwitch?.let {
                it?.setOnCheckedChangeListener { buttonView, isChecked ->
                    if(isChecked) {
                        showShifts()
                    }
                }
            }

        }
    }

其中showShifts如下(setupAvailabilities是我要切换的约束布局)

fun showShifts() {
    setupAvailabilities.visibility = View.VISIBLE
}

目前,availabilitySwitch 被评估为 null。谁能想到原因?

【问题讨论】:

标签: java android kotlin android-layout android-constraintlayout


【解决方案1】:

将您的逻辑移至onViewCreated 函数,availabilitiesSwitch 不应为空:

override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
    (activity as BaseActivity).run {

        availabilitiesSwitch?.setOnCheckedChangeListener { buttonView, isChecked ->
            if(isChecked) {
                showShifts()
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-12
    • 2022-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多