Android - How to make an progressBar touching a bottom screen border

前端 未结 5 542
春和景丽
春和景丽 2021-01-24 23:30

I want a ProgressBar that is touching a bottom border of screen but with this piece of code, I am getting an little space between bar and screen border:



        
相关标签:
5条回答
  • 2021-01-25 00:18

    Here is a hack that could work:

    • Create a LinearLayout for your whole view; set the width and height to fill_parent for it and orientation to vertical
    • Now, since you know how many components will be in your view, just give them the right amounts of weights; except the last progressbar
    • For the progressbar, give it the smallest of all; that way, it will be pushed all the way to the bottom;
    • Finally, using padding to reduce the space between it and the window (bottom)

    That is what I can think of unless you want to create a custom style for the progressbar as mentioned in a comment above

    0 讨论(0)
  • 2021-01-25 00:19

    I found a work around.I am basically adding a layout_marginBottom="-4dp" to my ProgressBar and wrapped it inside a RelativeLayout and aligned it to the bottom of the parent view.This changes may break your app in the future.For a better solution design a custom progress bar with your own custom drawable which you can align correctly and occupies less canvas space compared to the progressBarStyleHorizontal.

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true">
    
    <ImageButton
    android:id="@+id/imageButton”
    android:layout_width="40dip"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    android:contentDescription="@string/descrp"
    android:scaleType="center"
    android:src="@drawable/some_drawable”
    android:visibility="visible" />
    
    <TextView
    android:id="@+id/textview”
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="10dip"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:textColor="#FFFFFF"
    android:textStyle="bold" />
    //changes
    <RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <ProgressBar
        android:id="@+id/some_other_id"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="10dp"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="-4dp" />
    </RelativeLayout></RelativeLayout>
    

    Here's the link

    0 讨论(0)
  • 2021-01-25 00:31

    I have been struggling with this for a while now. My conclusion: There is no way to archieve this behaviour with XML for arbitrary screen sizes. On some screen sesolutions the progress bar will always be misplaced a little.

    My simple solution: Set the Y of the progressbar programmatically to the Y of the super view/layout

    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    
        progressBar.setY(layout.getY() - progressBar.getHeight() / 2);
    }
    

    Works like a charm and for all screen sizes.

    0 讨论(0)
  • 2021-01-25 00:32

    I came up with the solution to use a custom progressDrawable without any paddings like this:

    progress_bar_progress.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="@color/accent"/>
            </shape>
        </clip>
    </item>
    

    And then reference it in your ProgressBar:

    <ProgressBar
        android:layout_width="match_parent"
        android:layout_height="4dp"
        android:layout_alignParentBottom="true"
        android:background="#1A000000"
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminateOnly="false"
        android:progressDrawable="@drawable/progress_bar_progress" />
    
    0 讨论(0)
  • 2021-01-25 00:34

    Make your parent layout height as fill_parent and set progressbar height as match_parent.

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