首页 > 解决方案 > Android 复选框

问题描述

我正在尝试使用具有以下布局配置的复选框。

<android.support.v7.widget.AppCompatCheckBox
            android:id="@+id/gdprOptIn"
            fontPath="fonts/AvenirNextRegular.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="@dimen/medium_margin"
            android:layout_marginTop="@dimen/medium_margin"
            android:button="@drawable/checkbox"
            android:checked="false"
            android:paddingLeft="@dimen/default_margin"
            android:text="@string/opt_in_gdpr"
            android:textColor="@color/paper_ink_light"
            android:visibility="gone"
            android:gravity="top"/>

我找不到一种方法: - 避免文本可点击。我可以通过点击文本来选中/取消选中该框 - 在复选框周围添加圆形/方形轮廓,使其更易于使用。

任何想法 ?

标签: androidandroid-checkbox

解决方案


如果您不希望文本可点击,您可以将单个<CheckBox>标签替换为<LinearLayout>a<CheckBox>和 a <TextView>。例如,这个:

<CheckBox
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="hello world"/>

可以用这个代替:

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

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello world"/>

</LinearLayout>

如果您想在复选框周围绘制一个框以使其更加突出,您可以将其包裹在<FrameLayout>带有彩色背景的 a 中:

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

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#fac">

        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

    </FrameLayout>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="hello world"/>

</LinearLayout>

推荐阅读