删除水平进度条中的填充

时间:2016-11-10 15:35:57

标签: java android android-studio layout android-constraintlayout

在我们的应用程序中,我们需要一个不确定的进度条,如下所示:

progress bar how it should be

我们可以通过在ProgressBar上设置负边距来实现这一点,如下所示:

//...
$scope.showMap = function(mapID) {
    document.addEventListener("deviceready", function() {
        var div = document.getElementById("map" + mapID);
//...

但是因为ConstraintLayout不支持负边距,所以它看起来像这样:

progress bar with padding

好的,负利润是一个黑客。让我们换一个不同的黑客,好吗?让我们来介绍我们的自定义视图<ProgressBar android:id="@+id/progressbar" style="?android:attr/progressBarStyleHorizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:indeterminate="true" android:marginTop="-7dp" android:visibility="@{loading ? View.VISIBLE : View.GONE}" /> ,它扩展CustomProgressBar并覆盖其ProgressBar方法,如下所示:

onDraw

但所有这些都闻起来像坏代码。必须有一个更好的解决方案! 你会推荐什么?

6 个答案:

答案 0 :(得分:12)

感觉不像黑客的解决方案:将较小的ProgressBar包裹在较小的FrameLayout中。这样FrameLayout约束了它的高度,但ProgressBar仍然显示完整。

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

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

</FrameLayout>

答案 1 :(得分:6)

另一种方法是在父顶部和指南之间使用指南和中心ProgressBar。

<ProgressBar
    android:id="@+id/progressBar2"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="0dp"
    android:paddingTop="-4dp"
    android:layout_height="wrap_content"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/guideline"
    app:layout_constraintTop_toTopOf="parent" />

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline"
    android:orientation="horizontal"
    app:layout_constraintGuide_begin="4dp" />

答案 2 :(得分:2)

我也遇到了同样的问题。就像你说的那样,我遇到了许多解决方案,这些解决方案看起来都像是一个破坏其他东西的黑客。 话虽如此,我遇到了一个解决方案,即使用此library代替进度条。

需要注意的一点是,它告诉您通过添加以下内容来集成它:

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

然而,当我使用它时,它给我一个来自浮动操作栏的Android支持库的错误。所以我建议你改用它:

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

我如何使用它的示例代码段:

<me.zhanghai.android.materialprogressbar.MaterialProgressBar
    android:id="@+id/reviewProgressBar"
    style="@style/Widget.MaterialProgressBar.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="6dp"
    android:layout_below="@+id/my_toolbar"
    android:indeterminate="false"
    app:mpb_progressStyle="horizontal"
    app:mpb_useIntrinsicPadding="false"/>

希望这有帮助!

答案 3 :(得分:1)

我做的解决方法是将ProgressBar的高度设置为与笔划宽度紧密相似,如下所示:

进度条:

<ProgressBar
    android:id="@+id/pb_loading_progress"
    style="@style/Widget.AppCompat.ProgressBar.Horizontal"
    android:layout_width="match_parent"
    android:layout_height="2.1dp"
    android:layout_below="@id/tb_browser"
    android:max="100"
    android:progressDrawable="@drawable/style_browser_progress_drawable"
    android:visibility="invisible" />

Progress drawable:

<?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="line">
            <stroke
                android:width="2dp"
                android:color="@color/colorPrimary" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip
            android:clipOrientation="horizontal"
            android:gravity="left">
            <shape android:shape="line">
                <stroke
                    android:width="2dp"
                    android:color="@color/white" />
            </shape>
        </clip>
    </item>
</layer-list>

看起来像这样:

enter image description here

答案 4 :(得分:1)

在Linearlayout中使用,您将以简单的方式成功

 <LinearLayout
        android:layout_width="match_parent"
        android:background="#efefef"
        android:layout_height="wrap_content">

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

    </LinearLayout>

答案 5 :(得分:0)

您声明的视图没有任何明确的高度,因此它的高度是从预定义样式中选取的。

<ProgressBar
    android:id="@+id/progressbar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:indeterminate="true"
    android:marginTop="-7dp"
    android:visibility="@{loading ? View.VISIBLE : View.GONE}" />

将在当前主题中搜索“?android:attr / progressBarStyleHorizo​​ntal”,并将其存储在attrs.xml文件中。由于这是由平台定义的,因此它将从那里获取其价值。在撰写此答案时,我在android平台29上,搜索“?android:attr / progressBarStyleHorizo​​ntal”会得到以下结果

➜  values 
pwd
/Users/vihaanverma/Library/Android/sdk/platforms/android-29/data/res/values
➜  values 
grep -rin "progressBarStyleHorizontal" . 
./themes_device_defaults.xml:126:        <item name="progressBarStyleHorizontal">@style/Widget.DeviceDefault.ProgressBar.Horizontal</item>
./themes_device_defaults.xml:857:        <item name="progressBarStyleHorizontal">@style/Widget.DeviceDefault.Light.ProgressBar.Horizontal</item>
./themes.xml:272:        <item name="progressBarStyleHorizontal">@style/Widget.ProgressBar.Horizontal</item>
./themes_holo.xml:263:        <item name="progressBarStyleHorizontal">@style/Widget.Holo.ProgressBar.Horizontal</item>
./themes_holo.xml:626:        <item name="progressBarStyleHorizontal">@style/Widget.Holo.Light.ProgressBar.Horizontal</item>
./public.xml:148:  <public type="attr" name="progressBarStyleHorizontal" id="0x01010078" />
./themes_material.xml:258:        <item name="progressBarStyleHorizontal">@style/Widget.Material.ProgressBar.Horizontal</item>
./themes_material.xml:635:        <item name="progressBarStyleHorizontal">@style/Widget.Material.Light.ProgressBar.Horizontal</item>
./styles_material.xml:1010:        <item name="progressBarStyle">?attr/progressBarStyleHorizontal</item>
./attrs.xml:684:        <attr name="progressBarStyleHorizontal" format="reference" />
➜  values 

打开包含“ progressBarStyleHorizo​​ntal”的文件之一,您将看到该样式中定义的minHeight并等于16dp。

<style name="Widget.Material.ProgressBar.Horizontal" parent="Widget.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>
</style>

这是您获得额外填充的原因。您可以通过提供视图高度和下面的scaleY值来解决此问题

<ProgressBar
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="5dp"
    android:indeterminate="true"
    android:scaleY="5"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

enter image description here

相关问题