2016-03-26 43 views
-1

我買了一個ListView片段:空的ListView佈局在屏幕中間沒有顯示

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:layoutDirection="rtl" > 

<ListView 
    android:id="@+id/listViewPlaces" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 
</ListView> 

<ViewStub 
    android:id="@android:id/empty" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:layout="@layout/empty_list_layout" /> 

empty_list_layout

<Button 
    android:id="@+id/no_result_button" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:clickable="false" 
    android:drawableTop="@drawable/app_icon" 
    android:text="@string/empty_list" 
    android:textSize="30sp" /> 

我在Eclipse的圖形佈局中看到empty_list_layout的按鈕,但當列表爲空時,佈局顯示在屏幕的頂部。爲什麼它不像佈局所說的那樣在中間?

回答

0

這是因爲您的父視圖爲LinearLayout,其方向爲垂直,而您的空ViewStub的高度爲wrap_content。所以LinearLayout開始從上到下繪製它的孩子。

您想使用RelativeLayout作爲父項。

+0

爲什麼它不工作,如果我改變ViewStub的高度來匹配父母?其重力和其他佈局不應該工作? –

+0

實際上,'gravity'用於它的內部孩子,你想要使用的屬性是'layout_gravity',它會影響重力在其父項中的位置。 –

+0

,你可以從xml中看到:android:layout_gravity =「center」 –

相關問題