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