L预览的环形状不起作用

时间:2014-07-06 06:19:35

标签: android android-5.0-lollipop

只是测试新的开发人员预览,并注意到我现有的环形绘图无法正确渲染。而不是一个戒指,它是一个完整的圆圈。这是一个错误或有什么改变或我开始时做错了吗?任何可能的解决方案?

这是代码:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0">
            <solid android:color="#ff5698fb"/>
        </shape>
    </item>
</layer-list>

谢谢!

更新

我已按如下方式更新了形状:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0">
            <solid android:color="#ff5698fb"/>
        </shape>
    </item>
    <item >
        <shape
            android:innerRadiusRatio="4"
            android:shape="ring"
            android:thicknessRatio="5.5">
            <solid android:color="#ffffff"/>
        </shape>
    </item>
</layer-list>

通过覆盖两个圆圈产生一个环。但是我使用它的进度条不起作用。 以下是进度条的代码:

<ProgressBar
                android:id="@+id/progressCircle"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:indeterminate="false"
                android:max="100"
                android:progress="65"
                android:rotation="270"
                android:progressDrawable="@drawable/progress_circle"
                android:visibility="visible"/>

3 个答案:

答案 0 :(得分:21)

您需要在进度项中添加android:useLevel="true"。在以前的版本中,这似乎没有必要,但L想要它。

<item android:id="@android:id/progress">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="7.0"
        android:useLevel="true" >
        <solid android:color="#ff5698fb"/>
    </shape>
</item>

答案 1 :(得分:21)

TL; DR:为自定义进度条中使用的所有环形显式设置useLevel的值。


卡诺的答案是解决问题的方法。我正在补充它,以便在自定义进度条中添加有关环形状的一般信息。

似乎useLevel的默认值已经在Android版本上发生了变化。以下是与此相关的调查内容。

以下是使用响铃作为进度指示器的进度条的工作实现,以及另一个响铃作为进度背景:

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

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Progress background -->
    <item android:id="@android:id/background">
        <shape
            android:shape="ring"
            android:useLevel="false">
            <solid
                android:color="#dae1e6"/>
        </shape>
    </item>

    <!-- Progress -->
    <item android:id="@android:id/progress">
        <!-- Rotation of the progress to start at the top-->
        <rotate
            android:fromDegrees="-90"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="-90">
            <shape
                android:shape="ring"
                android:useLevel="true">
                <solid
                    android:color="#61bcf9"/>
            </shape>
        </rotate>
    </item>

</layer-list>

将其用作progressDrawable中的ProgressBar

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:progressDrawable="@drawable/custom_horizontal"
    android:progress="50"
    android:max="100"
    android:id="@+id/progressBar"
    android:layout_gravity="center"/>

useLevel=true设置为进度环形状,并将useLevel=false设置为背景环形状,为所有Android版本提供正确的所需行为:

Correct in both versions

从背景形状中删除useLevel

Pre 5.0(左)| 5.0(右)

Incorrect in KitKat Correct in Lollipop

从进度形状中删除useLevel

Pre 5.0(左)| 5.0(右)

Correct in KitKat Incorrect in Lollipop


因此,据我所知,useLevel用于指示形状是否受进度值影响。如果不是(假),它将被完全涂上;如果是(真),它将被绘制为与进度值一样多。

总结一下,似乎:

  • useLevel默认为 Android&lt;中的true 5.0 ,如果useLevel未设置为环形状,则会导致背景环跟随进度值,从而使其隐藏在进度指示器环的后面。
  • useLevel默认为 Android&gt; = 5.0 中的false,如果未在其中设置useLevel,则会完全绘制进度指示器环戒指形状。

因此,由于这种不一致性,最好在取决于进度的形状中明确设置useLeveltrue的值,并在那些不同的形状中明确设置false的值“T

此行为是基于我的观察,我没有找到任何来源确认。 The official Android docs对此并不十分清楚......

答案 2 :(得分:1)

只是想在框架源代码中添加一些引用,以确认在Android API版本中将useLevel默认为true&lt; 21(&lt; 5.0)并且在API版本中默认为false&gt; = 21(&gt; = 5.0),正如ChristianGarcía先前所述。

在android-20中,文件GradientDrawable.java,第820-835行:

if (shapeType == RING) {
    ....
    st.mUseLevelForShape = a.getBoolean(
                    com.android.internal.R.styleable.GradientDrawable_useLevel, true);
}

在android-21中,文件GradientDrawable.java,第1028-1047行:

if (state.mShape == RING) {
    ....
    state.mUseLevelForShape = a.getBoolean(
                    R.styleable.GradientDrawable_useLevel, state.mUseLevelForShape);
}
相关问题