复选框未显示

时间:2017-11-03 20:59:08

标签: android android-checkbox

我正在创建一个表单,其中我已经指定了一些EditText和一些TextView。最后我指定了一个像这样的Checkbox:

<CheckBox
                android:id="@+id/sendAsSMS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Send As SMS"
                android:checked="false"
                android:textSize="16sp"

                />

但是,当应用程序运行时,此复选框不会显示在布局中。这里出了什么问题。 我的完整layout.xml是:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.schoolcounter.ui.activity.teacher.AddHomeworkActivity">

    <LinearLayout
        android:id="@+id/addHomeworkForm"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="16dp">

        <LinearLayout
            android:id="@+id/selectClassLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/select_class"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingRight="@dimen/text_padding"
                android:paddingTop="@dimen/text_padding"
                android:paddingBottom="@dimen/text_padding"
                android:text="@string/select_class"
                android:textAlignment="textEnd"
                android:textColor="@color/colorPrimary"
                android:textStyle="bold"
                android:textSize="@dimen/text_size" />

            <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
                android:id="@+id/select_class_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="start"
                android:hint="@string/select_class"
                android:textAlignment="viewStart"
                android:textColorHint="@color/colorHint"
                android:textSize="@dimen/text_size" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/selectSectionLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/select_section"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingRight="@dimen/text_padding"
                android:paddingTop="@dimen/text_padding"
                android:paddingBottom="@dimen/text_padding"
                android:text="@string/select_section"
                android:textAlignment="textEnd"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size"
                android:textStyle="bold"/>

            <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
                android:id="@+id/select_section_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="start"
                android:hint="@string/select_section"
                android:textAlignment="viewStart"
                android:textColorHint="@color/colorHint"
                android:textSize="@dimen/text_size"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/selectSubjectLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/select_subject"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingRight="@dimen/text_padding"
                android:paddingTop="@dimen/text_padding"
                android:paddingBottom="@dimen/text_padding"
                android:text="@string/select_subject"
                android:textAlignment="textEnd"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size"
                android:textStyle="bold"/>

            <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
                android:id="@+id/select_subject_spinner"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="start"
                android:hint="@string/select_subject"
                android:textAlignment="viewStart"
                android:textColorHint="@color/colorHint"
                android:textSize="@dimen/text_size"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/uploadedDateLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView_uploaded_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingRight="@dimen/text_padding"
                android:paddingTop="@dimen/text_padding"
                android:paddingBottom="@dimen/text_padding"
                android:text="@string/text_uploaded_date"
                android:textAlignment="textEnd"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size"
                android:textStyle="bold"/>

            <EditText
                android:id="@+id/editTextUploadedDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="@dimen/text_size"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/completionDateLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView_completion_date"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="end"
                android:paddingRight="@dimen/text_padding"
                android:paddingTop="@dimen/text_padding"
                android:paddingBottom="@dimen/text_padding"
                android:text="@string/text_completion_date"
                android:textAlignment="textEnd"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size"
                android:textStyle="bold"/>

            <EditText
                android:id="@+id/editTextCompletionDate"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:textSize="@dimen/text_size"
                android:focusable="false"
                android:focusableInTouchMode="false"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <TextView
                android:id="@+id/textView_Homework"
                android:text="@string/text_homework"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:padding="@dimen/text_padding"
                android:textColor="@color/colorPrimary"
                android:textSize="@dimen/text_size"
                android:textStyle="bold"/>

            <EditText
                android:id="@+id/editTextHomeworkText"
                android:layout_width="match_parent"
                android:layout_height="74dp"
                android:ems="10"
                android:inputType="textMultiLine"
                android:textSize="@dimen/text_size"/>

            <CheckBox
                android:id="@+id/sendAsSMS"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Send As SMS"
                android:checked="false"
                android:textSize="16sp"

                />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:orientation="vertical"
            android:weightSum="1">


            <Button
                android:id="@+id/uploadHomeworkButton"
                style="@style/button_style_corners"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="50dp"
                android:layout_marginRight="50dp"
                android:layout_marginBottom="16dp"
                android:layout_marginTop="16dp"
                android:text="@string/text_upload"
                android:textAlignment="center"
                android:textColor="@color/colorWhite" />

        </LinearLayout>
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:1)

您必须在

中更改EditText'editTextHomeworkText'的width属性值
  

        <TextView
            android:id="@+id/textView_Homework"
            android:text="@string/text_homework"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="@dimen/text_padding"
            android:textColor="@color/colorPrimary"
            android:textSize="@dimen/text_size"
            android:textStyle="bold"/>

        <EditText
            android:id="@+id/editTextHomeworkText"
            android:layout_width="match_parent"
            android:layout_height="74dp"
            android:ems="10"
            android:inputType="textMultiLine"
            android:textSize="@dimen/text_size"/>

        <CheckBox
            android:id="@+id/sendAsSMS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Send As SMS"
            android:checked="false"
            android:textSize="16sp"

            />


    </LinearLayout>

由于您将宽度值指定为“match_parent”,因此EditText仅涵盖所有剩余空间。在这里,您可以将值更改为“wrap_content”或将其更改为

            android:layout_width="0dp"
            android:layout_weight="1"