可绘制作品不正确

时间:2019-03-31 12:28:12

标签: android drawable

好,我完全更新了我的问题。

我有一个只有黄色填充色的路径可绘制的xml:

<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FFD000"
        android:pathData="M19.43,12.98c0.04,-0.32 0.07,-0.64 0.07,-0.98s-0.03,-0.66 -0.07,-0.98l2.11,-1.65c0.19,-0.15 0.24,-0.42 0.12,-0.64l-2,-3.46c-0.12,-0.22 -0.39,-0.3 -0.61,-0.22l-2.49,1c-0.52,-0.4 -1.08,-0.73 -1.69,-0.98l-0.38,-2.65C14.46,2.18 14.25,2 14,2h-4c-0.25,0 -0.46,0.18 -0.49,0.42l-0.38,2.65c-0.61,0.25 -1.17,0.59 -1.69,0.98l-2.49,-1c-0.23,-0.09 -0.49,0 -0.61,0.22l-2,3.46c-0.13,0.22 -0.07,0.49 0.12,0.64l2.11,1.65c-0.04,0.32 -0.07,0.65 -0.07,0.98s0.03,0.66 0.07,0.98l-2.11,1.65c-0.19,0.15 -0.24,0.42 -0.12,0.64l2,3.46c0.12,0.22 0.39,0.3 0.61,0.22l2.49,-1c0.52,0.4 1.08,0.73 1.69,0.98l0.38,2.65c0.03,0.24 0.24,0.42 0.49,0.42h4c0.25,0 0.46,-0.18 0.49,-0.42l0.38,-2.65c0.61,-0.25 1.17,-0.59 1.69,-0.98l2.49,1c0.23,0.09 0.49,0 0.61,-0.22l2,-3.46c0.12,-0.22 0.07,-0.49 -0.12,-0.64l-2.11,-1.65zM12,15.5c-1.93,0 -3.5,-1.57 -3.5,-3.5s1.57,-3.5 3.5,-3.5 3.5,1.57 3.5,3.5 -1.57,3.5 -3.5,3.5z"/>
</vector>

enter image description here

然后我有一个带有复选框的片段(所有xml都很大,所以这里仅发布复选框,所有xml都在末尾):

<CheckBox
    android:id="@+id/checkbox_filter_tag_1"
    android:layout_width="32dp"
    android:layout_height="32dp"
    android:button="@drawable/ic_settings" />

代码左侧为黄色:

enter image description here

但是预览中为灰色,而实际设备中为灰色:

enter image description here

这一切都有效,直到我迁移到androidX和Single Activity。

如果我这样写,那就太奇怪了

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                app:srcCompat="@drawable/ic_settings"/>


            <CheckBox
                android:id="@+id/checkbox_filter_tag_1"
                android:layout_width="32dp"
                android:layout_height="32dp"
                android:button="@drawable/ic_settings" />

在代码,预览和真实设备中,它将像:

enter image description here

因此它可以在ImageView上使用,而不是在复选框中。

最后,我希望我的复选框与此选择器一起使用(禁用时为空,启用时填充),现在不起作用,因为所有都是灰色:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false">
        <layer-list>
            <!--<item android:width="16dp" android:height="16dp" android:gravity="center">-->
                <!--<shape android:shape="oval">-->
                    <!--<size android:width="10dp" android:height="10dp" />-->
                    <!--<solid android:color="@color/color_grey" />-->
                <!--</shape>-->
            <!--</item>-->

            <item android:width="16dp" android:height="16dp">
                <shape android:shape="oval">
                    <size android:width="15dp" android:height="15dp" />
                    <stroke android:width="1dp" android:color="@color/color_grey" />
                </shape>
            </item>
        </layer-list>
    </item>

    <item android:state_checked="true">
        <layer-list>
            <item android:width="10dp" android:height="10dp" android:gravity="center">
                <shape android:shape="oval">
                    <size android:width="10dp" android:height="10dp" />
                    <solid android:color="#c00"/>
                </shape>
            </item>

            <item android:width="16dp" android:height="16dp">
                <shape android:shape="oval">
                    <size android:width="15dp" android:height="15dp" />
                    <stroke android:width="1dp" android:color="@color/color_grey" />
                </shape>
            </item>
        </layer-list>
    </item>
</selector>

完整的xml(它很大,并且没有其他这样的主题或内容):

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <ScrollView
        android:id="@+id/root_filters"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="@dimen/margin_8"
            android:orientation="vertical"
            android:scrollbars="vertical">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:text="@string/filter_category"
                android:textColor="@android:color/black"
                android:textSize="@dimen/text_size_20"
                android:textStyle="bold" />

            <LinearLayout
                android:id="@+id/layout_filter_cat_1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_8"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/checkbox_filter_tag_1"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:button="@drawable/ic_settings" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="@dimen/margin_8"
                    android:text="@string/filter_1"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout_filter_cat_2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_8"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/checkbox_filter_tag_2"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:button="@drawable/checkbox_selector_design" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="@dimen/margin_8"
                    android:text="@string/filter_2"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout_filter_cat_3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_8"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/checkbox_filter_tag_3"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:button="@drawable/checkbox_selector_dev" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="@dimen/margin_8"
                    android:text="@string/filter_3"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />

            </LinearLayout>

            <LinearLayout
                android:id="@+id/layout_filter_cat_4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_8"
                android:orientation="horizontal">

                <CheckBox
                    android:id="@+id/checkbox_filter_tag_4"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:button="@drawable/checkbox_selector_managment" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_marginStart="@dimen/margin_8"
                    android:text="@string/filter_4"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />
            </LinearLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_12"
                android:layout_marginEnd="@dimen/margin_16"
                android:background="#c0c0c0" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_12"
                android:text="@string/filter_city"
                android:textColor="@android:color/black"
                android:textSize="@dimen/text_size_20"
                android:textStyle="bold" />

            <RadioGroup
                android:id="@+id/radiogroup_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_12">

                <RadioButton
                    android:id="@+id/radiobutton_city_1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:button="@drawable/checkbox_selector_place"
                    android:paddingLeft="@dimen/padding_16"
                    android:paddingRight="@dimen/padding_16"
                    android:text="@string/place_1"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/text_size_18" />

                <RadioButton
                    android:id="@+id/radiobutton_city_2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_16"
                    android:button="@drawable/checkbox_selector_place"
                    android:paddingLeft="@dimen/padding_16"
                    android:paddingRight="@dimen/padding_16"
                    android:text="@string/place_2"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/text_size_18" />

                <RadioButton
                    android:id="@+id/radiobutton_city_3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/margin_16"
                    android:button="@drawable/checkbox_selector_place"
                    android:paddingLeft="@dimen/padding_16"
                    android:paddingRight="@dimen/padding_16"
                    android:text="@string/place_3"
                    android:textColor="@android:color/black"
                    android:textSize="@dimen/text_size_18" />
            </RadioGroup>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_12"
                android:layout_marginEnd="@dimen/margin_16"
                android:background="#c0c0c0" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_big"
                android:layout_marginTop="@dimen/margin_default"
                android:layout_marginRight="@dimen/margin_big"
                android:visibility="gone">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="Бесплатно"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />

                <CheckBox
                    android:id="@+id/switch1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:button="@drawable/ic_switch_off" />

            </RelativeLayout>

            <RelativeLayout
                android:id="@+id/layout_filter_favorite"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/margin_big"
                android:layout_marginTop="@dimen/margin_default"
                android:layout_marginRight="@dimen/margin_big"
                android:visibility="gone">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_centerVertical="true"
                    android:text="Избранное"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18" />

                <CheckBox
                    android:id="@+id/switch2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:layout_centerVertical="true"
                    android:button="@drawable/ic_switch_off" />

            </RelativeLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginLeft="@dimen/margin_big"
                android:layout_marginTop="@dimen/margin_12"
                android:layout_marginRight="@dimen/margin_big"
                android:foreground="?android:attr/selectableItemBackground">

                <TextView
                    android:id="@+id/button_filter_close"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_vertical"
                    android:layout_weight="1"
                    android:gravity="center_horizontal"
                    android:text="Отмена"
                    android:textAllCaps="false"
                    android:textColor="@color/black"
                    android:textSize="@dimen/text_size_18"
                    android:textStyle="bold" />

                <RelativeLayout
                    android:id="@+id/button_filter_ok"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFD000"
                    android:foreground="?android:attr/selectableItemBackground"
                    android:orientation="horizontal"
                    android:padding="@dimen/padding_8">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerInParent="true"
                        android:text="@string/default_ok"
                        android:textColor="@color/black"
                        android:textSize="@dimen/text_size_18"
                        android:textStyle="bold" />
                </RelativeLayout>
            </LinearLayout>

            <View
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:layout_marginStart="@dimen/margin_16"
                android:layout_marginTop="@dimen/margin_12"
                android:layout_marginEnd="@dimen/margin_16"
                android:background="#c0c0c0" />

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

1 个答案:

答案 0 :(得分:0)

我记得我更改了checkbox-> androidx.appcompat.widget.AppCompatCheckBox,并添加了自己的buttonTint:

android:buttonTint="@color/checkbox_filters" 

现在一切正常。

checkbox_filters:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="#ff3" android:state_checked="true" />
    <item android:color="@color/gray" android:state_checked="false" />
</selector>