Remove vertical padding from horizontal ProgressBar

后端 未结 22 1857
眼角桃花
眼角桃花 2020-11-28 03:30

By default the ProgressBar has a certain padding above and below the bar itself. Is there a way to remove this padding so as to only have the bar in the end?

相关标签:
22条回答
  • 2020-11-28 04:09

    This is how I used Juozas's answer:

    height of my ProgressBar is 4dp. So I created a FrameLayout with height 4dp and set the layout_gravity of ProgressBar to center. It's works like a charm.

    <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>
    
    0 讨论(0)
  • 2020-11-28 04:09

    It's possible to draw vertically centered ProgressBar inside a parent that would clip away the padding. Since ProgressBar cannot draw itself bigger than parent, we must create a big parent to place inside a clipping view.

    <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>
    
    0 讨论(0)
  • 2020-11-28 04:09

    I'm using style="@style/Widget.AppCompat.ProgressBar.Horizontal" and it was fairly easy to get rid of the margins. That style is:

        <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>
    

    I just overrode the min/max height:

        <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" />
    
    0 讨论(0)
  • 2020-11-28 04:10

    I use minHeight and maxHeigh. It helps for different Api versions.

    <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" />
    

    It needs to use both. Api 23 works nice with

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

    But lower Api versions increase progress bar height to maxHeight in that case.

    0 讨论(0)
  • 2020-11-28 04:12

    A complete solution to this problem would be as follows. Just in case if someone needs code fragments, this is what I did.

    1. Copied all the 8 indeterminate horizontal progressbar drawables
    2. Edited the drawables using some image manipulator and remove unnecessary paddings
    3. Copied the drawable XML named progress_indeterminate_horizontal_holo.xml from android platform
    4. Copied the style Widget.ProgressBar.Horizontal and its parents
    5. Set the style and min_height manually in the layout

    Here is the progress_indeterminate_horizontal_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 resources copied to my local styles file.

    <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>
    

    And finally, set min height to 4dp in my local layout file.

    <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" />
    
    0 讨论(0)
  • 2020-11-28 04:12

    One trick is to add negative margins to your progress bar.

    Below is an example of the XML code, assuming it's on top of your screen:

    <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" />
    

    0 讨论(0)
提交回复
热议问题