从LayerDrawable内部的Drawable中删除填充

时间:2019-02-22 22:40:56

标签: android xml-drawable

我已经呆了几个小时,无法以编程方式更改“进度”和“ secondaryProgress”可绘制对象的填充。

我正在使用的Drawable xml资源如下。

<?xml version="1.0" encoding="utf-8"?>

<!--
  Progress Bar Drawable that is drawn vertically. This is basically a copy of
  progress_horizontal_material from the Android SDK's drawable, but modified
  to draw vertically instead of horizontally.
 -->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape android:shape="rectangle"android:tint="?colorControlNormal">
            <corners android:radius="@dimen/vertical_progress_bar_corner_radius" />
            <size android:width="@dimen/vertical_progress_bar_width" />
            <solid android:color="@color/white_disabled_progress" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape android:shape="rectangle" android:tint="?colorControlActivated">
                <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
                <corners android:radius="@dimen/vertical_progress_bar_corner_radius" />
                <size android:width="@dimen/vertical_progress_bar_width" />
                <solid android:color="@color/white_disabled_progress" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape android:shape="rectangle" android:tint="?colorControlActivated">
                <padding android:left="1dp" android:top="1dp" android:right="1dp" android:bottom="1dp" />
                <corners android:radius="@dimen/vertical_progress_bar_corner_radius" />
                <size android:width="@dimen/vertical_progress_bar_width" />
                <solid android:color="@android:color/white" />
            </shape>
        </clip>
    </item>
</layer-list>

1 个答案:

答案 0 :(得分:0)

我要寻找的忍者根本没有使用填充...。它使用了Insets而不是Padding。要清除“填充”,我必须通过java / kotlin清除Insets ...

如何通过java / kotlin清除插图(即padding):

val layerDarawble = drawable.mutate() as LayerDrawable // Important or else all instances of this drawable used in the app will be changed!
layerDrawable.setLayerInset(1, 0, 0, 0, 0)
layerDrawable.setLayerInset(2, 0, 0, 0, 0)

这是现在使用Insets而不是Padding的XML:

<?xml version="1.0" encoding="utf-8"?>

<!--
    Progress Bar Drawable that is drawn vertically. This is basically a copy of
    progress_horizontal_material from the Android SDK's drawable, but modified
    to draw vertically instead of horizontally.
-->

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape android:shape="rectangle" android:tint="?colorControlNormal">
            <corners android:radius="@dimen/vertical_progress_bar_corner_radius" />
            <size android:width="@dimen/vertical_progress_bar_width" />
            <solid android:color="@color/white_disabled_progress" />
        </shape>
    </item>

    <!--
        Note...insets are used instead of padding because I could not for the life of 
        me figure out how to clear the padding on this Drawable programmatically.
    -->
    <item android:id="@android:id/secondaryProgress"
          android:left="1dp"
          android:top="1dp"
          android:right="1dp"
          android:bottom="1dp">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape android:shape="rectangle" android:tint="?colorControlActivated">
                <corners android:radius="@dimen/vertical_progress_bar_corner_radius"/>
                <size android:width="@dimen/vertical_progress_bar_width"/>
                <solid android:color="@color/white_disabled_progress"/>
            </shape>
        </clip>
    </item>

    <!--
        Note...insets are used instead of padding because I could not for the life of me figure out
    how to clear the padding on this Drawable programmatically.
    -->
    <item android:id="@android:id/progress"
          android:left="1dp"
          android:top="1dp"
          android:right="1dp"
          android:bottom="1dp">
        <clip android:clipOrientation="vertical" android:gravity="bottom">
            <shape android:shape="rectangle" android:tint="?colorControlActivated">
                <corners android:radius="@dimen/vertical_progress_bar_corner_radius"/>
                <size android:width="@dimen/vertical_progress_bar_width"/>
                <solid android:color="@android:color/white"/>
            </shape>
        </clip>
    </item>

</layer-list>

...我知道我需要某种忍者。为什么要咒骂我,Android? :-|