我有一個網格(當前)有四個單元格寬。我指定的每個單元格寬度都是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嗎?網格中的列數和行數會有所不同,因此它周圍的空間應該動態調整大小。
這是一個exerpt?您至少缺少另一個線性佈局作爲所有這些視圖(垂直) – user3802077
的父級,您不需要引力(它會影響視圖內容而不影響其排列),Gridview周圍的視圖應具有固定高度或match_parent高度。此外你的linearlayout應該使用全屏的高度,因爲這是你想要的:)(match_parent) – user3802077
感謝您的回覆。我已經更新了代碼片段。我刪除了graivty並添加了LinearLayout。不知道複製和粘貼發生了什麼。對不起 – RNJ