从水平ProgressBar中删除垂直填充

问题描述 投票:104回答:18

默认情况下,ProgressBar在条形本身的上方和下方都有一定的填充。有没有办法删除这个填充,以便只有最后的栏?

android android-progressbar
18个回答
66
投票

我使用以下作为此问题的解决方法。

android:layout_marginBottom="-8dp"
android:layout_marginTop="-4dp"

2
投票

我用minHeight和maxHeigh。它有助于不同的Api版本。

<ProgressBar
    android:id="@+id/progress_bar"
    style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:maxHeight="3dp"
    android:minHeight="3dp" />

它需要同时使用。 Api 23很适合

android:layout_height="wrap_content"
android:minHeight="0dp"

但在这种情况下,较低的Api版本会将进度条高度增加到maxHeight。


2
投票

要删除ProgressBar的垂直填充,你可以做到

  • 修正ProgressBar的高度
  • 使用scaleY =“value”(value = height / 4)(4是进度条的默认高度)

示例包含1个wrap_content ProgressBar,1个8dp ProgressBar,1个100dp ProgressBar

enter image description here

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    ...
    android:layout_height="8dp"
    android:scaleY="2" />

1
投票

将android:progressDrawable添加到drawable中定义的图层列表中为我修复了这个问题。它通过在自定义drawable中屏蔽progess条来工作

https://stackoverflow.com/a/4454450/1145905描述的示例实现


1
投票

我正在使用style="@style/Widget.AppCompat.ProgressBar.Horizontal",它很容易摆脱边缘。那种风​​格是:

    <item name="progressDrawable">@drawable/progress_horizontal_material</item>
    <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal_material</item>
    <item name="minHeight">16dip</item>
    <item name="maxHeight">16dip</item>

我只是覆盖了最小/最大高度:

    <ProgressBar
        style="@style/Widget.AppCompat.ProgressBar.Horizontal"
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:indeterminate="true"
        android:minHeight="2dp"
        android:maxHeight="2dp" />

1
投票

请尝试以下方法:

<ProgressBar
    android:id="@+id/progress_bar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:progress="25"
    android:progressTint="@color/colorWhite"
    android:progressBackgroundTint="@color/colorPrimaryLight"
    android:layout_width="match_parent"
    android:layout_height="4dp" />

...然后根据您的需要配置进度条,因为它最初会显示一个带有黄色进度色调和灰色进度背景色调的中型条。另外,请注意没有垂直填充。


0
投票
 <ProgressBar
        android:layout_marginTop="-8dp"
        android:layout_marginLeft="-8dp"
        android:layout_marginRight="-8dp"
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:indeterminate="false"
        android:indeterminateTint="@color/white"
        android:max="100"
        android:paddingStart="8dp"
        android:paddingRight="0dp"
        android:progressDrawable="@drawable/progress_bg" />

0
投票

无需下载任何新模块,甚至不需要在Progress Bar中放置FrameLayout。这些都只是黑客攻击。只有两个步骤:

在你的whatever.xml中

<ProgressBar
    android:id="@+id/workoutSessionGlobalProgress"
    android:layout_width="match_parent"
    android:layout_height="YOUR_HEIGHT"
    android:progressDrawable="@drawable/progress_horizontal"
    android:progress="0"
    <!-- High value to make ValueAnimator smoother -->
    android:max="DURATION * 1000"
    android:indeterminate="false"
    style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal"/>

progress_horizo​​ntal.xml,根据需要更改值。

不喜欢圆角? 删除角半径

不喜欢这些颜色?改变颜色等完成!

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

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
                    />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#80ffd300"
                        android:centerColor="#80ffb600"
                        android:centerY="0.75"
                        android:endColor="#a0ffcb00"
                        android:angle="270"
                        />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                        android:startColor="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                        />
            </shape>
        </clip>
    </item>

</layer-list>

通常,这些是更改您不喜欢的任何代码的步骤。只需找到源代码并找出要更改的内容。如果您遵循ProgressBar源代码,您将找到它引用的名为progress_horizo​​ntal.xml的文件。基本上我是如何解决所有XML问题的。


0
投票

最好的解决方案应该是

android:minHeight="0dp"

没有解决方法,就像魅力一样。


-1
投票

一个简单的无技巧解决方案,与任何版本的Android兼容,不需要外部库,在ProgressBar中使用两个Views伪装LinearLayout。这就是我最终的结果。看起来非常整洁,这种方法非常灵活 - 您可以用时髦的方式为其添加动画,添加文本等。

布局:

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

    <TextView
        android:id="@+id/inventory_progress_value"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <TextView
        android:id="@+id/inventory_progress_remaining"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</LinearLayout>

码:

public void setProgressValue(float percentage) {
    TextView progressValue = (TextView) findViewById(R.id.inventory_progress_value);
    TextView progressRemaining = (TextView) findViewById(R.id.inventory_progress_remaining);

    LinearLayout.LayoutParams paramsValue = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    LinearLayout.LayoutParams paramsRemaining = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    paramsValue.weight = (100 - percentage);
    paramsRemaining.weight = percentage;

    progressValue.setLayoutParams(paramsValue);
    progressRemaining.setLayoutParams(paramsRemaining);

}

结果(添加了一些高程):

enter image description here


48
投票

这就是我使用Juozas的答案:

我的ProgressBar的高度是4dp。所以我创建了一个高度为4dp的FrameLayout,并将layout_gravityProgressBar设置为center。它就像一个魅力。

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="4dp">

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_gravity="center"
        android:indeterminate="true"/>

</FrameLayout>

30
投票

我最终使用自定义库来解决此问题。大多数其他解决方案都有效,但各种设备的结果并不一致。

MaterialProgressBar

  • 在Android 4.0+上保持一致的外观。
  • 跨平台校正着色。
  • 能够删除框架ProgressBar的内在填充。
  • 能够隐藏框架水平ProgressBar的轨道。
  • 用作框架ProgressBar的替代品。

要添加为gradle依赖项:

compile 'me.zhanghai.android.materialprogressbar:library:1.1.7'

要在布局中添加没有内在填充的ProgressBar,请执行以下操作:

<me.zhanghai.android.materialprogressbar.MaterialProgressBar
    android:layout_width="wrap_content"
    android:layout_height="4dp"
    android:indeterminate="true"
    app:mpb_progressStyle="horizontal"
    app:mpb_useIntrinsicPadding="false"
    style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal" />

app:mpb_useIntrinsicPadding="false"做到了这一点。有关更多详细信息,请参阅GitHub page


14
投票

可以在父级内部绘制垂直居中的ProgressBar,以便剪掉填充。由于ProgressBar无法将自己绘制得比父级更大,因此我们必须创建一个大父级来放置在剪切视图中。

<FrameLayout
    android:id="@+id/clippedProgressBar"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="4dp"
    tools:ignore="UselessParent">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="16dp"
        android:layout_gravity="center_vertical">

        <ProgressBar
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:indeterminate="true"/>

    </FrameLayout>

</FrameLayout>

13
投票

该问题的完整解决方案如下。如果有人需要代码片段,这就是我所做的。

  1. 复制了所有8个不确定的水平进度条抽屉
  2. 使用一些图像操纵器编辑drawables并删除不必要的填充
  3. 从android平台复制名为progress_indeterminate_horizo​​ntal_holo.xml的可绘制XML
  4. 复制样式Widget.ProgressBar.Horizo​​ntal及其父级
  5. 在布局中手动设置样式和min_height

这是progress_indeterminate_horizo​​ntal_holo.xml

<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
<item android:drawable="@drawable/progressbar_indeterminate_holo1" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo2" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo3" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo4" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo5" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo6" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo7" android:duration="50" />
<item android:drawable="@drawable/progressbar_indeterminate_holo8" android:duration="50" />

样式资源复制到我的本地样式文件。

<style name="Widget">
    <item name="android:textAppearance">@android:attr/textAppearance</item>
</style>

<style name="Widget.ProgressBar">
    <item name="android:indeterminateOnly">true</item>
    <item name="android:indeterminateBehavior">repeat</item>
    <item name="android:indeterminateDuration">3500</item>
</style>

<style name="Widget.ProgressBar.Horizontal">
    <item name="android:indeterminateOnly">false</item>
    <item name="android:indeterminateDrawable">@drawable/progress_indeterminate_horizontal_holo</item>
</style>

最后,在我的本地布局文件中将min height设置为4dp。

<ProgressBar
    android:id="@+id/pb_loading"
    style="@style/Widget.ProgressBar.Horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:indeterminate="true"
    android:minHeight="4dp"
    android:minWidth="48dp"
    android:progressDrawable="@drawable/progress_indeterminate_horizontal_holo" />

7
投票

如果有人仍然需要帮助可以试试这个:

<androidx.core.widget.ContentLoadingProgressBar
                android:id="@+id/progress"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.AppCompat.ProgressBar.Horizontal"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="@+id/guideline" android:indeterminate="true"
                android:visibility="visible" app:layout_constraintBottom_toTopOf="@+id/guideline"/>

这里,进度条位于ConstraintLayout内,并且constraintTop_toTopOf和constraintBottom_toTopOf属性必须应用于同一元素(在本例中,它是指南)。


6
投票

使用带有水平样式的进度条时遇到了同样的问题。

根本原因是进度条的默认9-patch drawable:(progress_bg_holo_dark.9.png)有一些垂直透明像素作为填充。

对我有用的最终解决方案:自定义可绘制的进度,我的示例代码如下:

custom_horizo​​ntal_progressbar_drawable.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 android:shape="rectangle">
            <solid android:color="#33ffffff" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="rectangle">
                <solid android:color="#ff9800" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <solid android:color="#E91E63" />
            </shape>
        </clip>
    </item>
</layer-list>

布局代码段:

<ProgressBar
    android:id="@+id/song_progress_normal"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:layout_alignParentBottom="true"
    android:progressDrawable="@drawable/custom_horizontal_progressbar_drawable"
    android:progress="0"/>

5
投票

Subin的答案似乎是唯一一个(目前)并不是一个脆弱的黑客,可能会在Android ProgressBar的未来版本中出现破坏。

但是,我没有经历过分解资源,修改资源和无限期维护资源的麻烦,而是选择使用MaterialProgressBar library,它为我们做到了:

<me.zhanghai.android.materialprogressbar.MaterialProgressBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:layout_gravity="bottom"
    custom:mpb_progressStyle="horizontal"
    custom:mpb_showTrack="false"
    custom:mpb_useIntrinsicPadding="false"
    style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal.NoPadding"
/>

在build.gradle中:

// Android horizontal ProgressBar doesn't allow removal of top/bottom padding
compile 'me.zhanghai.android.materialprogressbar:library:1.1.6'

该项目有一个nice demo,显示它与内置ProgressBar之间的差异。


3
投票

一个技巧是在进度条中添加负边距。

下面是XML代码的示例,假设它位于屏幕顶部:

<ProgressBar
    android:id="@+id/progressBar"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="-7dp"
    android:layout_marginBottom="-7dp"
    android:indeterminate="true" />

result of code

© www.soinside.com 2019 - 2024. All rights reserved.