2012-09-05 51 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

相關問題