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

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

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:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"  android:layout_height="match_parent"  tools:context=".Whatever" >  <ProgressBar     android:id="@+id/progressBar1"     style="?android:attr/progressBarStyleHorizontal"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentBottom="true"     android:layout_alignParentLeft="true"     android:layout_alignParentRight="true" />  </RelativeLayout> 

And here is what I am getting:

is there any way to remove that space?

回答1:

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



回答2:

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



回答3:

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



回答4:

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.



回答5:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!