2012-09-09 36 views
1

我有一個ListView它有兩列,我想要的是中心ListViewLinerlayout。這裏是ListView的佈局代碼。如何在一個LinearLayout在一個ListView居中android

<?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" 
    android:gravity="center" > 
    <ListView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:id="@+id/mylist"> 
    </ListView> 
</LinearLayout> 

這裏是的ListView

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="horizontal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <TextView 
     android:id="@+id/prayLabel" 
     android:layout_width="100dip" 
     android:layout_height="wrap_content" /> 
    <TextView 
     android:id="@+id/prayValue" 
     android:layout_width="100dip" 
     android:layout_height="wrap_content" /> 
</LinearLayout> 

雖然我ListView個別項目的佈局出現垂直居中,但它沒有出現水平居中作爲其寬度橫跨的整個寬度屏幕。我想我在layout_width的所有位置使用wrap_content,其寬度不應該跨越屏幕/佈局的整個寬度?

感謝

+0

添加此在你的第二個xml'android:gravity =「center」'在線性佈局 – MAC

+0

@ gtumca-MAC中添加了它,但它沒有任何區別:(。 – Jawed

回答

3

我將它包裝在一個RelativeLayout爲simplicity-我也爲此努力奮鬥,並最終在簡單的佈局,這樣做**:

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

     <RelativeLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 

      <!-- Note the 'centre in parent' tag below --> 

      <ListView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:id="@+id/mylist"/> 

     </RelativeLayout> 

    </LinearLayout> 

**免責聲明:本模式可以成爲昂貴的更復雜的意見,但LinearLayout>RelativeLayout>ListView層次結構是好的。

+0

那麼儘管它確實在ListView中的內容居中, ListView的寬度保持不變,即跨越佈局的整個寬度,這是我不想要的,我希望ListView的寬度減少到行的最大寬度(即在我的情況下爲200) – Jawed

+0

你有沒有試着將''''''''''''''''''''''''把''''''''''''''''將'''''''''包裝到'''''wrap_content'''上?''''我總是以一種試驗和錯誤的方式。如果這不起作用,那麼在將RelativeLayout設置爲wrap_content之後,將列表視圖設置爲''''match_parent''',即匹配RelativeLayout的寬度。 – OceanLife

0

您可以在您的線性佈局中使用android:weightSum,然後用空的觀點一左一右中心您ListView如下:

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:weightSum="1"> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".05"/> 
    <ListView 
     android:id="@+id/list_view" 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".9" 
     android:paddingLeft="4dp" 
     android:paddingRight="4dp" /> 
    <View 
     android:layout_width="0dp" 
     android:layout_height="wrap_content" 
     android:layout_weight=".05"/> 
</LinearLayout> 

當然,你可以調整權重,僅僅指剛確保Views設定爲相同所以ListView居中。

相關問題