[英]how to set android progress bar in center of the screen
I am trying to show the progress bar in the center of the screen, but it doesn't show up in the center. 我正在尝试在屏幕中央显示进度条,但它没有显示在中央。 Below is my layout.
下面是我的布局。 What am i doing wrong here?
我在这里做错了什么? Basically this is a layout to show image and android gallery widget on top.
基本上,这是一个在顶部显示图像和android gallery小部件的布局。 When the user clicks on the thumbnails, I am loading an image from the web and I want to show the progress dialog while the image is loading.
当用户单击缩略图时,我正在从Web加载图像,并且希望在加载图像时显示进度对话框。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="@+id/container" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="vertical">
<Gallery android:id="@+id/gallery" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="top" />
<ImageView android:id="@+id/actualimage" android:scaleType="fitCenter"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:visibility="gone" android:layout_gravity="center_vertical|center_horizontal|center"
android:adjustViewBounds="true" />
<ProgressBar android:id="@+android:id/progress_small1"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:visibility="visible" android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal" />
</LinearLayout>
Using a LinearLayout
will stack your views vertically. 使用
LinearLayout
将垂直堆叠您的视图。 Right now, your ImageView
and ProgressBar
are fighting for the centered position. 现在,您的
ImageView
和ProgressBar
正在争取居中位置。 For this type of situation, I would definitely recommend a RelativeLayout
. 对于这种情况,我绝对会推荐
RelativeLayout
。
With RelativeLayout
, you use android:layout_centerInParent="true"
. 通过
RelativeLayout
,您可以使用android:layout_centerInParent="true"
。
Do android:gravity="center"
in the root LinearLayout
. 在根
LinearLayout
执行android:gravity="center"
。 Set the android:visibility
of all other elements apart from the ProgressBar
to gone
when you want to show the ProgressBar
设置
android:visibility
从分开的所有其他元素的ProgressBar
来gone
,当你想要显示的ProgressBar
Add following: 添加以下内容:
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
Maybe you should go for progress dialog, since I believe you are trying to show a progress indicator until the images are loaded for the gallery. 也许您应该进入进度对话框,因为我相信您正在尝试显示进度指示器,直到为图库加载图像为止。
This will get you started. 这将使您入门。
http://www.helloandroid.com/tutorials/using-threads-and-progressdialog http://www.helloandroid.com/tutorials/using-threads-and-progress对话框
http://www.androidpeople.com/android-progress-dialog-example http://www.androidpeople.com/android-progress-dialog-example
And still if you are looking for a way to make use of your progressbar itself then maybe you should give a try to the answer provided by Bradley Uffner 而且,如果您正在寻找一种利用进度条本身的方法,那么也许您应该尝试一下Bradley Uffner提供的答案
尝试将进度条的layout_weight设置为1
I know this question is answered, but considering performance of laying out views/Subviews, RelativeLayout is expensive(it requires two pass). 我知道这个问题已经解决,但是考虑到布局视图/子视图的性能,RelativeLayout昂贵(需要两次通过)。 If your view hierarchy is complex, go for Relative Layout, or if your view has just simple views like below please go for Linear layout/Frame layout.
如果您的视图层次结构很复杂,请使用“相对布局”,或者如果您的视图只有以下简单视图,请使用“线性布局/框架”布局。
From my experience, when my app grown, UI performance with Relative layout was worse, ended up in reworking on all layout files :( Below is sample for putting a view(ProgressView in my case) in exactly center of view. 根据我的经验,当我的应用程序增长时,相对布局的UI性能会变差,最终会在所有布局文件上进行重新加工:(以下是将视图(在我的情况下为ProgressView)放置在视图中心的示例。
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:orientation="vertical">
<android.support.v7.widget.AppCompatImageView android:layout_gravity="center" android:layout_width="match_parent"
android:layout_height="0dp" app:srcCompat="@drawable/check" android:scaleType="fitCenter"
android:layout_weight=".7"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp" android:layout_weight=".3">
<ProgressBar android:layout_gravity="center|center_horizontal" android:indeterminateTint="@color/textColorPrimary"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:indeterminate="true"/>
</FrameLayout>
</LinearLayout>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.