2015-10-07 16 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> 
相關問題