带内边距的 Android 进度条

发布于 2024-12-28 04:44:34 字数 1424 浏览 2 评论 0原文

我想让我的进度条看起来像这样:

在此处输入图像描述

我不想使用图像,所以我有尝试用形状来做这个:

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:centerColor="#2a2723"
            android:centerY="0.75"
            android:endColor="#2a2723"
            android:startColor="#2a2723" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:centerColor="#16e61c"
                android:centerY="0.75"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>

但我无法为绿线设置填充(我尝试在任何可能的地方设置它),而且我也无法制作这样的圆角(看起来 corners android:radius 不起作用)。请帮我

I want to make my progress bar looks something like that:

enter image description here

I dont want to use images, so I have tried to make this with the shapes:

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:centerColor="#2a2723"
            android:centerY="0.75"
            android:endColor="#2a2723"
            android:startColor="#2a2723" />
    </shape>
</item>
<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:centerColor="#16e61c"
                android:centerY="0.75"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>
<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="50dp" />

            <gradient
                android:angle="270"
                android:endColor="#9dfd6e"
                android:startColor="#16e61c" />
        </shape>
    </clip>
</item>

But I cant set padding for the green line (Ive tried to set its everywhere where it was possible), and also I can't make such round corners (looks like corners android:radius isn't working). Please help me

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

城歌 2025-01-04 04:44:34
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#222" />
            <corners android:radius="10dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <layer-list>
                <item>
                    <color android:color="#00000000" />
                </item>
                <item
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp">
                    <shape>
                        <solid android:color="#00FF00" />
                        <corners android:radius="10dp" />
                    </shape>
                </item>
            </layer-list>
        </clip>
    </item>
</layer-list>

查看我的博客

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#222" />
            <corners android:radius="10dp" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <layer-list>
                <item>
                    <color android:color="#00000000" />
                </item>
                <item
                    android:left="5dp"
                    android:top="5dp"
                    android:right="5dp"
                    android:bottom="5dp">
                    <shape>
                        <solid android:color="#00FF00" />
                        <corners android:radius="10dp" />
                    </shape>
                </item>
            </layer-list>
        </clip>
    </item>
</layer-list>

see my blog

一百个冬季 2025-01-04 04:44:34

一个简单的解决方法是将进度条包装到具有圆形形状且背景颜色与进度条相同的布局中。

示例进度条可绘制 (drawable/progress_bar.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="50dip" />
            <solid android:color="@color/white"/>
            <padding android:bottom="3dip" android:left="3dip" android:right="3dip" android:top="3dip" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <bitmap android:src="@drawable/test_progress_bar_progress" android:tileMode="repeat"/>
    </item>
</layer-list>

进度条样式:

<resources>
    <style name="ProgressBarStyle" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">10dip</item>
        <item name="android:max">100</item>
        <item name="android:progressDrawable">@drawable/progress_bar</item>
    </style>
</resources>

布局形状 (drawable/rounded_shape.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <padding android:top="2dip" android:left="2dip" android:right="2dip" android:bottom="2dip"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
    <solid android:color="@color/white"/>
</shape>

活动布局:

<!-- ... -->
      <LinearLayout                     
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/rounded_shape"
                  android:layout_marginLeft="30dip"
                  android:layout_marginRight="30dip">
            <ProgressBar
                    android:id="@+id/testSummaryProgressBar"
                    android:progress="10"
                    style="@style/ProgressBarStyle"/>
        </LinearLayout>
<!-- ... -->

效果:

在此处输入图像描述

要增加进度条的填充,您必须增加 rounded_shape 文件中的填充。

A simple workaround is to wrap progress bar into layout which has rounded shape and same background color as progress bar.

Sample progress bar drawable (drawable/progress_bar.xml):

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="50dip" />
            <solid android:color="@color/white"/>
            <padding android:bottom="3dip" android:left="3dip" android:right="3dip" android:top="3dip" />
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <bitmap android:src="@drawable/test_progress_bar_progress" android:tileMode="repeat"/>
    </item>
</layer-list>

Progress bar style:

<resources>
    <style name="ProgressBarStyle" parent="@android:style/Widget.ProgressBar.Horizontal">
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">10dip</item>
        <item name="android:max">100</item>
        <item name="android:progressDrawable">@drawable/progress_bar</item>
    </style>
</resources>

Layout shape (drawable/rounded_shape.xml):

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <padding android:top="2dip" android:left="2dip" android:right="2dip" android:bottom="2dip"/>
    <corners android:bottomRightRadius="7dp" android:bottomLeftRadius="7dp" android:topLeftRadius="7dp" android:topRightRadius="7dp"/>
    <solid android:color="@color/white"/>
</shape>

Activity layout:

<!-- ... -->
      <LinearLayout                     
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:background="@drawable/rounded_shape"
                  android:layout_marginLeft="30dip"
                  android:layout_marginRight="30dip">
            <ProgressBar
                    android:id="@+id/testSummaryProgressBar"
                    android:progress="10"
                    style="@style/ProgressBarStyle"/>
        </LinearLayout>
<!-- ... -->

Effect:

enter image description here

To increase padding of progress bar you have to increase padding in rounded_shape file.

兮子 2025-01-04 04:44:34

是的,不使用@Nykakin 建议的图像就可以做到这一点。要为进度添加内边距圆角,就是对项目进度使用列表层。

<item android:id="@android:id/progress">
<clip>
  <layer-list>
       <-- transparent background  -->
   <item>
       <color android:color="#00000000" />
   </item>
   <-- padding -->
   <item  
      android:left="2dp"
      android:top="2dp"
      android:right="2dp"
      android:bottom="2dp">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:endColor="#9dfd6e"
            android:startColor="#16e61c" />
     </shape>
   </item>
  </layer-list>
</clip>

Yes it's possible to have exactly that without using image like @Nykakin has suggested. To have the padding and the rounded corner for the progress is to use list-layer for the item progress.

<item android:id="@android:id/progress">
<clip>
  <layer-list>
       <-- transparent background  -->
   <item>
       <color android:color="#00000000" />
   </item>
   <-- padding -->
   <item  
      android:left="2dp"
      android:top="2dp"
      android:right="2dp"
      android:bottom="2dp">
    <shape>
        <corners android:radius="50dp" />

        <gradient
            android:angle="270"
            android:endColor="#9dfd6e"
            android:startColor="#16e61c" />
     </shape>
   </item>
  </layer-list>
</clip>

花开雨落又逢春i 2025-01-04 04:44:34

不幸的是我没有找到解决我的问题的方法。我可以制作圆角的唯一方法是使用圆角 9 间距图像

有一个很好的教程可以做到这一点:http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/

Unfortunately I didn't find solution for my problem. The only way I could make round corners was to use rounded 9-pitch image

There is a good tutorial to do that: http://blog.mediarain.com/2011/04/android-custom-progressbar-with-rounded-corners/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文