Android自定义复选框选择器问题

时间:2016-04-23 10:18:33

标签: android layout checkbox

我必须自定义一个复选框,以便在选中时显示2个不同的状态,我使用以下2个图像:

enter image description here

enter image description here

我的自定义选择器定义如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/favourite_normal" android:state_checked="false"/>
<item android:drawable="@drawable/favourite_choose" android:state_checked="true"/>
<item android:drawable="@drawable/favourite_normal"/>

但事情是我没有得到正确的结果,因为如果我改变我必须做的调整的复选框的高度和宽度,图像不能正确填充我的复选框视图。结果我得到这样的结果:

enter image description here

完整的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="74dp"
        android:background="@drawable/bottom_bav_btn">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="fill_parent"
                    android:id="@+id/jokeListViewBackButton"
                    android:src="@drawable/back_screen_button_normal"
                    android:layout_margin="10dp" />
            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_toRightOf="@+id/linearLayout"
                android:layout_toLeftOf="@+id/linearLayout2"
                android:layout_toStartOf="@+id/linearLayout2"
                android:weightSum="1">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="37dp"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="Large Text"
                        android:id="@+id/jokeTitleTextView"
                        android:gravity="center_vertical|center_horizontal"
                        android:textColor="@color/fontWhiteColor" />

                    <TextView
                        android:layout_width="fill_parent"
                        android:layout_height="37dp"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:text="0/0"
                        android:id="@+id/jokeCountTxt"
                        android:gravity="center_vertical|center_horizontal"
                        android:textColor="@color/fontWhiteColor" />
                </LinearLayout>

            </LinearLayout>

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="80dp"
                android:layout_height="match_parent"
                android:layout_alignParentTop="true"
                android:layout_alignParentRight="true"
                android:layout_alignParentEnd="true"
                android:id="@+id/linearLayout2"
                android:gravity="center_vertical|center_horizontal">

                <CheckBox
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:id="@+id/checkBox"
                    android:button="@drawable/custom_favourite_selector"
                    android:gravity="center_vertical|center_horizontal"
                    android:checked="false" />
            </LinearLayout>
        </RelativeLayout>

    </LinearLayout>


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:weightSum="1"
        android:background="@color/listViewBody"
        android:layout_weight="0.71">

        <ScrollView
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:id="@+id/scrollView" >

            <com.gravityapps.framework.Utils.CustomTextView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:text=""
                android:id="@+id/jokeTextView"
                android:layout_below="@+id/imageView"
                android:layout_centerHorizontal="true"
                android:gravity="center"
                android:textSize="25dp"
                android:layout_alignParentBottom="true"
                android:layout_marginRight="10dp"
                android:layout_marginLeft="10dp" />
        </ScrollView>

    </LinearLayout>

    <LinearLayout android:id="@+id/navbar"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:orientation="horizontal">

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/prevButtonLayout"
            android:clickable="true">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView2"
                android:background="@drawable/previous_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/randPrevButtonLayout"
            android:clickable="true" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView3"
                android:background="@drawable/reverse_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/linearLayout5"
            android:clickable="true" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView4"
                android:background="@drawable/copy_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/linearLayout6"
            android:clickable="true" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView7"
                android:background="@drawable/share_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/randNextButtonLayout"
            android:clickable="true" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView5"
                android:background="@drawable/forward_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:clickable="true"
            android:layout_weight="1"
            android:background="@drawable/bottom_nav_back"
            android:id="@+id/nextButtonLayout">

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:id="@+id/imageView6"
                android:background="@drawable/next_btn_normal"
                android:layout_margin="15dp" />
        </LinearLayout>

    </LinearLayout>
</LinearLayout>

我不确定为什么我会得到这个结果。任何帮助,将不胜感激。感谢

2 个答案:

答案 0 :(得分:2)

Checkbox 40dp * 40dp的尺寸小于您在选择器中定义的图像。

使用wrap_content代替静态尺寸。

答案 1 :(得分:1)

根据之前的回答,如果您不希望复选框变大,则必须调整图像大小。我认为这些是您最好的选择,要么 wrap_content 您的复选框宽度和高度,要么您将图像资源调整为较小的,例如。 40 * 40像素