在片段中选择微调项时复选框消失

Checkbox disappears when spinner item is selected in Fragment

我有一个片段,其中有一个微调器和复选框。

布局文件need_help_fragment_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/all_fragment_background"
    android:clickable="true"
    android:focusable="true">
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/parent_top"
        app:layout_constraintGuide_percent="0"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/parent_bottom"
        app:layout_constraintGuide_percent="1"
        android:orientation="horizontal"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/parent_left"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0"/>
    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/parent_right"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="1"/>

    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/top_title_bottom"
        app:layout_constraintGuide_percent=".25"
        android:orientation="horizontal"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/top_title_bottom"
        app:layout_constraintEnd_toStartOf="@+id/parent_right"
        app:layout_constraintStart_toEndOf="@+id/parent_left"
        app:layout_constraintTop_toBottomOf="@+id/parent_top">
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_top"
            app:layout_constraintGuide_percent="0"
            android:orientation="horizontal"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_bottom"
            app:layout_constraintGuide_percent="1"
            android:orientation="horizontal"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_left"
            android:orientation="vertical"
            app:layout_constraintGuide_percent="0.07"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/text_view_right"
            android:orientation="vertical"
            app:layout_constraintGuide_percent=".93"/>
        <TextView
            android:text="@string/need_help_header_text"
            android:paddingLeft="10dp"
            android:paddingRight="10dp"
            android:id="@+id/title_header_text_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintTop_toBottomOf="@+id/text_view_top"
            app:layout_constraintBottom_toTopOf="@+id/text_view_bottom"
            app:layout_constraintStart_toEndOf="@+id/text_view_left"
            app:layout_constraintEnd_toStartOf="@+id/text_view_right"
            android:textColor="@color/all_text_color"
            android:textSize="18sp"
            android:gravity="center">
        </TextView>
    </androidx.constraintlayout.widget.ConstraintLayout>


    <androidx.constraintlayout.widget.Guideline
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:id="@+id/mid_content_bottom_guide"
        app:layout_constraintGuide_percent=".8"
        android:orientation="horizontal"/>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintTop_toBottomOf="@+id/top_title_bottom"
        app:layout_constraintBottom_toTopOf="@+id/mid_content_bottom_guide"
        app:layout_constraintStart_toEndOf="@+id/parent_left"
        app:layout_constraintEnd_toStartOf="@+id/parent_right">
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/mid_content_top"
            app:layout_constraintGuide_percent="0"
            android:orientation="horizontal"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/mid_content_bottom"
            app:layout_constraintGuide_percent="1"
            android:orientation="horizontal"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/mid_content_left"
            android:orientation="vertical"
            app:layout_constraintGuide_percent=".07"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/mid_content_right"
            android:orientation="vertical"
            app:layout_constraintGuide_percent=".93"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/spinner_bottom"
            app:layout_constraintGuide_percent=".2"
            android:orientation="horizontal"/>

        <androidx.appcompat.widget.AppCompatSpinner
            android:background="@null"
            app:layout_constraintTop_toBottomOf="@+id/mid_content_top"
            app:layout_constraintBottom_toTopOf="@+id/spinner_bottom"
            app:layout_constraintStart_toEndOf="@+id/mid_content_left"
            app:layout_constraintEnd_toStartOf="@+id/mid_content_right"
            android:id="@+id/spinner_view"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:gravity="center_horizontal | bottom"
            android:text="3 Seconds"
            android:textColor="@color/all_text_color"
            android:textSize="22sp" />

        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/checkbox_top"
            app:layout_constraintGuide_percent=".4"
            android:orientation="horizontal"/>
        <androidx.constraintlayout.widget.Guideline
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:id="@+id/checkbox_bottom"
            app:layout_constraintGuide_percent=".6"
            android:orientation="horizontal"/>
        <CheckBox
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:text="Remove timing, Auto Mode"
            app:layout_constraintTop_toBottomOf="@+id/checkbox_top"
            app:layout_constraintBottom_toTopOf="@+id/checkbox_bottom"
            app:layout_constraintStart_toEndOf="@+id/mid_content_left"
            app:layout_constraintEnd_toStartOf="@+id/mid_content_right"
            android:layoutDirection="rtl"
            android:gravity="center_vertical"
            android:paddingLeft="10dp"
            android:textSize="18sp"
            android:textColor="@color/all_text_color"
            android:id="@+id/remove_timing_auto_mode_check_box"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

片段中的代码:

class NeedHelpFragment: Fragment(), View.OnClickListener {
    lateinit var homeActivity : MainActivity
    var check_box: CheckBox? = null
    var spinner_view: Spinner? = null
    lateinit var timeSpinnerList: ArrayList<String>
    lateinit var timeSpinnerAdapter: ArrayAdapter<String>

    override fun onAttach(context: Context) {
        super.onAttach(context)
        if(context is MainActivity)
            homeActivity = context
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        var v : View = inflater.inflate(R.layout.need_help_fragment_layout, container, false)
        check_box = v.findViewById<CheckBox>(R.id.check_box)
        timeSpinnerList = ArrayList()
        for(i in 0..10)
            timeSpinnerList.add("$i seconds")
        timeSpinnerAdapter = ArrayAdapter(homeActivity, android.R.layout.simple_spinner_dropdown_item, timeSpinnerList)
        spinner_view = v.findViewById<Spinner>(R.id.spinner_view)
        spinner_view!!.adapter = timeSpinnerAdapter
        return v
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
    }

    override fun onClick(view: View?) {
        when(view!!.id){

        }
    }
}

颜色代码:

<color name="colorPrimary">#6200EE</color>
<color name="colorPrimaryDark">#3700B3</color>
<color name="colorAccent">#03DAC5</color>
<color name="all_text_color">#6F6F6F</color>
<color name="all_fragment_background">#F8F8F8</color>

只要没有 selected 就可以了:

当从微调器中 select 编辑一个项目时,复选框消失:

布局文件中没有其他内容。只是约束布局以及复选框和微调器的指南。微调器和复选框在片段的 onCreateView 中初始化。当我 select 微调器中的任何项目时,复选框会消失。请帮忙。

我认为 问题 是关于 constraintLayout 选项的设置值,例如 :

app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/spinner_view"
app:layout_constraintBottom_toBottomOf="parent"

我认为在某些情况下错误

然后为了简单的例子,我删除了指南并解决了它。

您可以使用此方法并添加您的指南。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/all_fragment_background"
    android:clickable="true"
    android:focusable="true">

    <TextView
        android:id="@+id/title_header_text_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="120dp"
        android:gravity="center"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="HEAD"
        android:textColor="@color/all_text_color"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <androidx.appcompat.widget.AppCompatSpinner
        android:id="@+id/spinner_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:background="@null"
        android:gravity="center_horizontal|bottom"
        android:text="3 Seconds"
        android:textColor="@color/all_text_color"
        android:textSize="22sp"
        app:layout_constraintBottom_toTopOf="@+id/remove_timing_auto_mode_check_box"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/title_header_text_view" />


    <CheckBox
        android:id="@+id/remove_timing_auto_mode_check_box"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:layoutDirection="rtl"
        android:paddingLeft="10dp"
        android:text="Remove timing, Auto Mode"
        android:textColor="@color/all_text_color"
        android:textSize="18sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/spinner_view" />


</androidx.constraintlayout.widget.ConstraintLayout>