How to set ProgressBar at the center screen in frame layout?
I am using following code, but my progress bar is not displaying in the center of frame layout. I want to avoid padding to my progressbar.Can any one please explain? It is perfectly working if i am using "Linear Layout"
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http:开发者_StackOverflow中文版//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal">
<ProgressBar android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView android:id="@+id/gallery_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
In the layout xml for your progressBar set the android:layout_gravity
(instead of android:gravity
) to "center"
android:layout_gravity = "center"
Use
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true"
Example:
<ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_centerInParent="true" />
android:gravity
attribute is not supported on ProgressBar
FrameLayout
does not have android:gravity
. You need to set android:layout_gravity
on the ProgressBar
instead.
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
***android:layout_gravity="center"*** />
add this line in
<ProgressBar
android:layout_gravity="center" />
Just change your Layout as RelativeLayout and add android:layout_centerInParent="true" to your ProgressBar.
<ProgressBar
android:id="@+id/progBar"
android:indeterminateDrawable="@drawable/my_progress_indeterminate"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:layout_width="50dp">
</ProgressBar>
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerInParent="true"
/>
As of 2018, you need to set the below attribute:
android:layout_centerInParent="true"
Working solution as of 2017
android:layout_gravity="center"
you must set the postion in pixels as in framelayout different components overlap each other. So, you can try it by setting pixels in dip.
精彩评论