Remove vertical padding from horizontal ProgressBar

后端 未结 22 1854
眼角桃花
眼角桃花 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:02

    Not necessary to download any new module or even put a FrameLayout around your Progress Bar. These are all just hacks. Only 2 steps:

    In your 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_horizontal.xml, Change the values as you please.

    Don't like the rounded corners?
    Remove corner radius

    Don't like the colors? Change the colors, etc. Done!

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

    Generally, these are the steps to change the code of anything you don't like. Just find the source code and figure out what to change. If you follow the ProgressBar source code, you will find a file called progress_horizontal.xml that it references. Basically how I solve all my XML problems.

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

    The best solution should be

    android:minHeight="0dp"
    

    No workaround and works like a charm.

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

    I met the same problem while using progressbar with Horizontal style.

    The root cause is that the default 9-patch drawable for progress bar: (progress_bg_holo_dark.9.png) has some vertical transparent pixels as padding.

    The final Solution that worked for me: customize the progress drawable, my sample code as follow:

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

    layout snippet:

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

    Subin's answer seems to be the only one (currently) that isn't a fragile hack subject to breakage in future releases of the Android ProgressBar.

    But rather than going through the trouble of breaking out the resources, modifying them, and maintaining them indefinitely, I've opted to use the MaterialProgressBar library, which does that for us:

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

    In build.gradle:

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

    That project has a nice demo that shows the differences between it and the built-in ProgressBar.

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

    A simple no-tricks solution which is compatible with any version of Android and doesn't need external libraries is faking ProgressBar with two Views inside LinearLayout. This is what I ended up with. Looks pretty neat and this approach is quite flexible - you can animate it in funky ways, add text etc.

    Layout:

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

    Code:

    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);
    
    }
    

    Result (with some elevation added):

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

    Use like this, inside 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>
    
    0 讨论(0)
提交回复
热议问题