2011-10-24 37 views
11

我想在屏幕中心显示进度条,但它不会显示在屏幕中间。以下是我的布局。我在这里做错了什么?基本上这是一个布局,显示图像和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> 

回答

33

使用所提供的答案LinearLayout将垂直堆叠您的视图。现在,你的ImageViewProgressBar正在为中心位置而战。对于这种情况,我肯定会推荐RelativeLayout

使用RelativeLayout,您使用android:layout_centerInParent="true"

+1

或者只是使用Progr ess Dialog,由Andro Selva推荐(取决于造型需求/喜好) –

+2

Relative Layout为我做了诀窍。非常感谢 – Chandu

2

先给进度条的1

+0

将不得不在这一个与雄激素塞尔瓦同意。进度对话框将会BA很多更容易使用。 –

+0

感谢您的意见,我以为我会是什么我尝试添加layout_weight更新。对于一些原因是它将进度条的大小扩展到了丑陋的像素化轮子旋转,layout_weight更适合屏幕空间在视图之间分配的比例,而不是我认为的 – Chandu

3

android:gravity="center"在根LinearLayout。设置除了ProgressBargone所有其他元素的android:visibility当你想显示ProgressBar

+0

感谢您的意见。我确实尝试过,但我仍然需要展示AdMob adView,因此它没有像预期的那样工作。 – Chandu

+0

这应该以问题标题回答 – Thecave3

2

添加以下内容:

android:layout_centerHorizontal="true" 
android:layout_centerVertical="true" 
0

我知道这个问题的答案,但考虑铺设意见/子视图的性能,RelativeLayout是昂贵的(它需要两遍)。如果您的视图层次结构很复杂,请参阅相对布局,或者如果您的视图只有下面的简单视图,请转到线性布局/框架布局。

根据我的经验,当我的应用程序增长时,相对布局的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>