2012-09-05 50 views
0

我一直在搜索解决方案,我只找到解决方案,其中FrameLayoutRelativeLayout被使用,并且我正在使用LinearLayoutAndroid - 如何设置以屏幕为中心的前景图像?

我想要做的是在页面上加载一些内容之前,在前台显示一个图片“请稍等”。我试图让图片居中,而不是移动其他页面元素......所以我想我需要它在前台,对吧?

我该怎么做?现在我有这个:

<ImageView 
    android:id="@+id/please_wait" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/please_wait" 
    android:foregroundGravity="center|fill_horizontal" /> 

但它不是居中形象,并没有把它放在前台。

这里是整个XML文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     > 

<include android:id="@+id/header" 
     layout="@layout/header" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent"/>  

<ImageView 
     android:id="@+id/please_wait" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/please_wait" 
     android:foregroundGravity="center|fill_horizontal" 
     /> 

<TextView 
    android:id="@+id/problem_loading_prompt"  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Getting the business(es) you are planning. They are stored in a database that is constantly backed up so you do not lose your work." 
    />   

    <ListView 
    android:id="@android:id/list" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@+id/label" 
     android:textSize="17sp"> 
    </ListView> 

     <Button 
    android:id="@+id/add_problem_ok" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="@color/light_best_blue" 
    android:text="Plan a New Business" 
    android:layout_marginTop ="15dp"  
    /> 
</LinearLayout> 

谢谢!

+0

可以粘贴到整个xml文件pls。 –

+0

@Rahmathullah M Pulikkal只是粘贴它 - 谢谢:) – Genadinik

+0

所以其他组件将被加载时隐藏。对? –

回答

2

我认为最好的方式来显示“请稍候”的消息刚刚创建对话框。在这里:http://developer.android.com/guide/topics/ui/dialogs.html你有很好的教程如何创建一个对话框。我认为你可以在自定义视图或ProgressDialog中使用AlertDialog,因为它具有美容微调器。

你从java代码调用的对话框,不是来自xml,所以Dialog对你的问题没有严格的回答,但我认为它接近你想要的。

如果你真的想在XML中做这样的事情,我认为你必须使用FrameLayout,因为它被设计为做类似的事情。或者你可以尝试使用RelativeLayout。

编辑: 这里是你的图像创建AlertDialog示例代码:

Dialog dialog = new Dialog(context); 
dialog.setContentView(R.layout.custom); 
dialog.show(); //this will show dialog 
dialog.dismiss(); //now dialog will disapear 

在这里,你的XML文件名为custom.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

<ImageView 
    android:id="@+id/please_wait" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/please_wait"/> 

</RelativeLayout> 

当然你也可以使用不同的布局也。

1

为什么不尝试这样的事:

<RelativeLayout> 

    <LinearLayout> 
     <Your existing stuff /> 
    </LinearLayout> 

    <ImageView 
     android:centerInParent="true" /> 

</RelativeLayout> 

这种方式可以让你做的整个布局到现在为止,并RelativeLayout将很好地把你的图像上的一切之上。如果您不需要再ImageView,只需拨打

imageView.setVisibility(View.INVISIBLE /*or View.GONE */); 
+0

它是否必须处于相对布局?我正在使用线性布局,并且您提出的建议,我喜欢,但由于某种原因,不幸的是,它不能用于线性布局。任何想法这里最好的办法是什么?我应该切换到相对布局吗? – Genadinik

+0

嗨!人们向你推荐RelativeLayout,因为它可以让你把东西放在最上面。 LinearLayout绝不会将其子项放在彼此之上。我试图写这个例子,你不必重写你现有的布局。您只需将现有的应用程序布局封装到LinearLayout中,然后添加ImageView,然后将所有内容放入RelativeLayout中。 –

1

你试过这样吗?

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:gravity="center" >    <--- like this 

<ImageView 
    android:id="@+id/please_wait" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:foregroundGravity="center|fill_horizontal" 
    android:src="@drawable/arrow" /> 

</LinearLayout> 

它适合我。

+0

我用android:gravity =“center”这样试过,但它在屏幕上有一个奇怪的效果,并且稍微移动了一些东西,但仍然没有将图像放在前景中,并居中。 – Genadinik

相关问题