Android 进度条设置在屏幕中央的方法
我试图在屏幕中央显示进度条,但它没有显示在中央。下面是我的布局。我在这里做错了什么?基本上,这是一个在顶部显示图像和 Android 图库小部件的布局。当用户单击缩略图时,我正在从网络加载图像,并且我想在加载图像时显示进度对话框。
<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>
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. 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.
<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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用
LinearLayout
将垂直堆叠您的视图。现在,您的ImageView
和ProgressBar
正在争夺居中位置。对于这种情况,我肯定会推荐RelativeLayout
。对于
RelativeLayout
,您可以使用android:layout_centerInParent="true"
。Using a
LinearLayout
will stack your views vertically. Right now, yourImageView
andProgressBar
are fighting for the centered position. For this type of situation, I would definitely recommend aRelativeLayout
.With
RelativeLayout
, you useandroid:layout_centerInParent="true"
.在根
LinearLayout
中执行android:gravity="center"
。当您想要显示ProgressBar
时,将除ProgressBar
之外的所有其他元素的android:visibility
设置为gone
Do
android:gravity="center"
in the rootLinearLayout
. Set theandroid:visibility
of all other elements apart from theProgressBar
togone
when you want to show theProgressBar
尝试将进度条的layout_weight设置为1
Try giving the progress bar a layout_weight of 1
也许您应该使用进度对话框,因为我相信您正在尝试显示进度指示器,直到为图库加载图像为止。
这将帮助您开始。
http://www.helloandroid.com/tutorials/using-threads-and- Progressdialog
http://www.androidpeople.com/android-progress-dialog-example
如果您正在寻找一种利用进度条本身的方法,那么也许您应该尝试一下答案由布拉德利乌夫纳提供
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.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
添加以下内容:
Add following:
我知道这个问题已经得到解答,但是考虑到布局视图/子视图的性能,RelativeLayout 很昂贵(它需要两次传递)。如果您的视图层次结构很复杂,请选择相对布局,或者如果您的视图只有如下所示的简单视图,请选择线性布局/框架布局。
根据我的经验,当我的应用程序增长时,相对布局的 UI 性能会更差,最终会重新处理所有布局文件:( 下面是将视图(在我的情况下为 ProgressView)放在视图中心的示例。
I know this question is answered, but considering performance of laying out views/Subviews, RelativeLayout is expensive(it requires two pass). 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.