2012-09-05 33 views
3

我有這樣一個XML文件:的Android進度的.xml佈局

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

    <ProgressBar 
     android:id="@+id/progressbar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_vertical|center_horizontal" 
     android:visibility="visible" /> 

    <WebView 
     android:id="@+id/web_engine" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:visibility="gone" /> 

</LinearLayout> 

我想ProgressBarLarge風格,並在屏幕的中間垂直和水平居中......當我將layout_widthheight設置爲fill_parent,它將實際加載微調器的大小增加到整個屏幕......我如何使佈局適合整個屏幕並居中實際加載圖標?

+0

試試android:layout_gravity =「center」 – VendettaDroid

回答

8

我檢查了幾種可能性,在我的經驗,這樣的任務最好的選擇是:

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

    <ProgressBar 
     android:id="@+id/progressbar" 
     style="?android:attr/progressBarStyleLarge" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerInParent="true" 
     android:visibility="visible" /> 

    <WebView 
     android:id="@+id/web_engine" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true"  
     android:visibility="gone" /> 

</RelativeLayout> 

我使用RelativeLayoutandroid:layout_centerInParent="true"其作品比LinearLayout更好。

Cheers

+0

完美的作品,'RelativeLayout'效果更好!謝謝 – tcd

3

您還可以使用LinearLayout的FrameLayout插入。這種可見的視圖將重疊不可見的視圖。我修改了你的代碼:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

<ProgressBar 
    android:id="@+id/progressbar" 
    style="?android:attr/progressBarStyleLarge" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical|center_horizontal" 
    android:visibility="visible" /> 

<WebView 
    android:id="@+id/web_engine" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:visibility="gone" /> 

</FrameLayout> 
+0

也適用!謝謝 – tcd