2017-03-04 71 views
2

enter image description here XAML中,我能做到这一点很容易的Android什么网格布局使用

<Grid> 
    <Grid.RowDefinition> 
     <RowDefinition Height="*" /> 
     <RowDefinition Height="auto" /> 
    </Grid.RowDefinition> 

    <!--#region canvas --> 
    <Grid Grid.Row="0"> 
    </Grid> 
    <!--#endregion --> 

    <!--#region parameters --> 
    <Grid Grid.Row="1"> 
    </Grid> 
    <!--#endregion --> 
</Grid> 

我移动到Android开发的第一时间,我不知道哪种类型的“小部件”,以使用具有与上述相同的网格布局行为(屏幕截图)

如何制作具有相同布局行为的网格?

回答

0

通常的方式实现这一目标是使用的LinearLayout和分配权重1和0的两个相应的元件,和高度“0dp”和“WRAP_CONTENT”相应地:

<LinearLayout 
    ... 
    android:layout_height="match_parent" 
    android:layout_width="match_parent" 
    android:orientation="vertical"> 
    <RelativeLayout 
     ... 
     android:layout_height="0dp" 
     android:layout_width="match_parent" 
     android:layout_weight="1"> 
    ... 
    </RelativeLayout> 
    <RelativeLayout 
     ... 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_weight="0"> 
    ... 
    </RelativeLayout> 
</LinearLayout> 

(凡RelativeLayout的需要,以被你的元素取代)。

方式layout_heightlayout_weight的工作如下:LinearLayout内的元素一般将具有与其权重成比例的高度。因此,例如,如果两个元素的权重为1和2,则第二个元素的高度将是第一个元素的两倍。

但是,该元素不会小于其layout_height

现在,考虑到以上两点,上面的XML有效地说“我希望第一个元素比第二个元素大无比大,但第二个元素不能小于包含其子元素所需的元素”正是你想要的。

您可能还会在网上使用layout_height="fill_parent"发现一些文章。它也可以工作,但这是一个不赞成使用的解决方案,应该通常优先采用layout_weight的解决方案。

+0

谢谢!这样可行! – jaysonragasa

+0

@ jaysonragasa,如果答案有效,你应该接受:) – Ishamael

+0

我以为我已经做到了 – jaysonragasa