0

我有一個網格(當前)有四個單元格寬。我指定的每個單元格寬度都是20px。我想把它放在肖像佈局的中間和頂部。 我的代碼如下。我的問題是,網格似乎填滿了整個寬度,而它希望它只需要列數*列寬度。併爲它周圍的空白空間。 我已經把顏色放在每個組件上去嘗試,並且很容易看到每個組件的位置。第一個LinearLayout中的兩個視圖從不顯示。我試過中間佈局動態網格

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:tools="http://schemas.android.com/tools" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:paddingLeft="@dimen/activity_horizontal_margin" 
      android:paddingRight="@dimen/activity_horizontal_margin" 
      android:paddingTop="@dimen/activity_vertical_margin" 
      android:paddingBottom="@dimen/activity_vertical_margin" 
      android:orientation="vertical" 
      tools:context=".EntryPointFragment"> 

<LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

    <View 
      android:layout_width="wrap_content" 
      android:background="#FFFFFF00" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="1"/> 

    <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
       android:id="@+id/grid_view" 
       android:background="#FF000000" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:numColumns="4" 
       android:columnWidth="30dp" 
       android:verticalSpacing="1dp" 
       android:horizontalSpacing="1dp" 
       android:gravity="center"/> 

    <View 
      android:layout_width="wrap_content" 
      android:background="#FFFF00FF" 
      android:layout_height="wrap_content" 
      android:gravity="right" 
      android:layout_weight="1"/> 
</LinearLayout> 

<View 
     android:layout_width="match_parent" 
     android:background="#FF00FFFF" 
     android:layout_height="match_parent" 
     android:gravity="bottom" 
     android:layout_weight="1"/> 

</LinearLayout> 

這張圖片顯示了我想要的東西。

任何人都可以幫我調整這個XML嗎?網格中的列數和行數會有所不同,因此它周圍的空間應該動態調整大小。

感謝 My Ideal Layout

+0

這是一個exerpt?您至少缺少另一個線性佈局作爲所有這些視圖(垂直) – user3802077

+0

的父級,您不需要引力(它會影響視圖內容而不影響其排列),Gridview周圍的視圖應具有固定高度或match_parent高度。此外你的linearlayout應該使用全屏的高度,因爲這是你想要的:)(match_parent) – user3802077

+0

感謝您的回覆。我已經更新了代碼片段。我刪除了graivty並添加了LinearLayout。不知道複製和粘貼發生了什麼。對不起 – RNJ

回答

1

嘗試:

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:orientation="horizontal" 
     android:layout_weight="1" 
     android:gravity="center" 
     android:background="#ffff00"> 

     <View 
      android:layout_width="0dp" 
      android:background="#ff0000" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

     <GridView 
      android:id="@+id/grid_view" 
      android:background="#ffffff" 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:numColumns="4" 
      android:columnWidth="20dp" 
      android:verticalSpacing="1dp" 
      android:horizontalSpacing="1dp"/> 

     <View 
      android:layout_width="0dp" 
      android:background="#FFFF00FF" 
      android:layout_height="wrap_content" 
      android:layout_weight="1"/> 

    </LinearLayout> 

    <View 
     android:layout_width="match_parent" 
     android:background="#FF00FFFF" 
     android:layout_height="0dp" 
     android:layout_weight="3"/> 

</LinearLayout> 

你會得到這樣的事情:

Example of wanted layout

BTW:你可以改變你的GridView的寬度/高度你希望,你也可以用類似的方法來放置它旁邊的視圖。

+0

感謝@Matcoil的答案。我確實想改變寬度和高度,這取決於數據源中的數據。我將使用layout_width/layout_height來查看是否可以根據需要獲取它。理想情況下,我希望單元格寬度爲20像素,寬度和高度自動計算取決於數據 – RNJ