Android - 如何设置屏幕中心的前景图像?

时间:2012-09-05 18:54:23

标签: android android-layout

我一直在谷歌搜索解决方案,我只找到使用FrameLayoutRelativeLayout的解决方案,而我正在使用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>

谢谢!

3 个答案:

答案 0 :(得分: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

这里有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 :(得分:1)

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

<RelativeLayout>

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

    <ImageView
        android:centerInParent="true" /> 

</RelativeLayout>

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

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

答案 2 :(得分: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>

它对我有用。