0

我一直在谷歌搜索解决方案,我只找到使用FrameLayout或的解决方案RelativeLayout,并且我正在使用LinearLayout.

我要做的是在页面上加载某些内容之前在前景中显示“请稍候”的图像。而且我试图让该图像居中,而不是移动其他页面元素......所以我想我需要它在前台,对吗?

我怎么能这样做?现在我有这个:

<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>

谢谢!

4

3 回答 3

2

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

您从 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

这里有一个名为 custom.xml 的 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>

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

于 2012-09-05T19:17:00.020 回答
1

为什么不尝试这样的事情:

<RelativeLayout>

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

    <ImageView
        android:centerInParent="true" /> 

</RelativeLayout>

通过这种方式,您可以保留到现在所做的整个布局,并且RelativeLayout可以很好地将您的图像放在所有内容之上。如果您不再需要ImageView,只需致电

imageView.setVisibility(View.INVISIBLE /*or View.GONE */);
于 2012-09-05T19:14:41.077 回答
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>

这个对我有用。

于 2012-09-05T19:19:33.470 回答