2015-10-07 15 views
0

我是新来的Android编程,我想做一个表格/网格(一般来说,不确定最好使用布局)。我希望桌子能够填满整个屏幕,但是我想为单元格设置可变大小的高度和宽度。例如,我希望第一列是显示宽度的1/3,第二列是显示宽度的2/3。同样,我希望某些单元格的高度为屏幕高度的1/4,而其他单元格的高度为屏幕高度的1/2。等等。最佳布局是什么?我正在看GridLayout,LinearLayout和TableLayout。创建一个网格的单元高度和宽度相对于屏幕尺寸

Tx!

回答

1

您可以使用LinearLayout轻松完成。

其设置在然后,设置你的第一个元素在0.3 layout_weight和你的第二个在0.66 layout_weight。然后它会给你的第一个元素1/3或屏幕,其余的给你的第二个。

为每个元素放置一个0dp的width。然后根据你的元素的重量,你的元素将在屏幕上传播。

希望它有帮助。

0

有两种方法可供选择:

1)如果你知道你所有的项目将和可以预先定义它们。

我会在LinearLayouts中使用LinearLayouts(父方向设置为垂直,子设置为水平)。然后在父级(例如3)上使用weight_sum,并在子级(例如2,1)上使用layout_weight。将children的layout_width设置为0dp。

2)如果你正在尝试表示数据

这是一个稍微复杂的变量列表,但你可能是最好关闭使用带有GridLayoutManager一个RecyclerView这将允许您以编程方式设置基于类型的项目范围。

编辑:

的#1实施例:

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView 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" 
    tools:context=".MainActivity"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="4"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="3" 
       android:layout_height="wrap_content" 
       android:background="#abc"/> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="1" 
       android:layout_height="wrap_content" 
       android:background="#cda"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="4"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="2" 
       android:layout_height="wrap_content" 
       android:background="#af0"/> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="2" 
       android:layout_height="wrap_content" 
       android:background="#ffa"/> 
     </LinearLayout> 
     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="4"> 
      <TextView 
       android:layout_width="0dp" 
       android:layout_weight="4" 
       android:layout_height="wrap_content" 
       android:background="#f00"/> 
     </LinearLayout> 
    </LinearLayout> 
</ScrollView> 
相关问题