2012-09-05 30 views
12

Tôi đang phát triển ứng dụng "phù hợp với sau". Xml được sử dụng cho ứng dụng như sau:Android: Cách tạo GridLayout [không phải GridView] theo lập trình?

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lytLinear" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/txtQuestion" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="30dp" 
     android:text="Match the following questions with their answers." /> 

    <GridLayout 
     android:id="@+id/lytGrid" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:columnCount="3" 
     android:columnWidth="300dp" 
     android:orientation="vertical" 
     android:rowCount="5" 
     android:stretchMode="columnWidth" > 

     <LinearLayout 
      android:id="@+id/Q1Lyt" 
      android:layout_width="160dp" 
      android:layout_height="50dp" 
      android:layout_column="0" 
      android:layout_row="0" 
      android:background="@drawable/shape_qn" 
      android:gravity="center" > 

      <TextView 
       android:id="@+id/Q1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_column="0" 
       android:layout_row="0" 
       android:text="Question1" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/A1Lyt" 
      android:layout_width="160dp" 
      android:layout_height="50dp" 
      android:layout_column="1" 
      android:layout_row="0" 
      android:background="@drawable/shape" 
      android:gravity="center" 
      android:text="Answer1" > 

      <TextView 
       android:id="@+id/A1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_column="0" 
       android:layout_row="0" 
       android:text="Answer1" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/Q2Lyt" 
      android:layout_width="160dp" 
      android:layout_height="50dp" 
      android:layout_column="0" 
      android:layout_row="1" 
      android:background="@drawable/shape_qn" 
      android:gravity="center" > 

      <TextView 
       android:id="@+id/Q2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="Question2" /> 
     </LinearLayout> 

     <LinearLayout 
      android:id="@+id/A2Lyt" 
      android:layout_width="160dp" 
      android:layout_height="50dp" 
      android:layout_column="1" 
      android:layout_row="1" 
      android:background="@drawable/shape" 
      android:gravity="center" > 

      <TextView 
       android:id="@+id/A2" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_column="0" 
       android:layout_row="0" 
       android:text="Answer2" /> 
     </LinearLayout> 
    </GridLayout> 
</LinearLayout> 

Tôi đã thử đoạn mã sau để tạo bố cục lưới. Tuy nhiên, đối với bố cục tuyến tính con, tôi không thể tìm cách thiết lập các tham số android: layout_column và android: layout_row theo cách lập trình.

GridLayout gridLayout = new GridLayout(this); 
    gridLayout.setColumnCount(2); 
    gridLayout.setRowCount(15); 

    LinearLayout lyt1 = new LinearLayout(this); 
    LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); 

Mọi con trỏ?

+0

Tôi chưa thể tìm hiểu thêm thông tin. Do đó, tôi đang sử dụng bố cục bảng thay thế. – Srikanth

+10

Tôi không hiểu tại sao câu hỏi này đã bị đóng. Tôi nghĩ rằng đó là một lỗi. Nó sẽ giúp tôi nếu câu hỏi được trả lời. – Softlion

+4

var gridLayoutParams = new GridLayout.LayoutParams(); gridLayoutParams.SetGravity (GravityFlags.Fill); gridLayoutParams.RowSpec = GridLayout.InvokeSpec (iRow, 1); gridLayoutParams.ColumnSpec = GridLayout.InvokeSpec (iCol, 1); grid.AddView (aView, gridLayoutParams); – Softlion

Trả lời

-3

Bạn nên cố gắng này:

gridLayout.setLayoutParams(prmsLinLyt); 

và nếu bạn muốn cung cấp cho các giá trị ...

LinearLayout.LayoutParams prmsLinLyt = new LinearLayout.LayoutParams(90,100); 

where layout_width = 90 
and layout_height= 100 
+0

Cảm ơn tôi biết điều đó. Tuy nhiên, tôi không thể tìm thấy tương đương cho thông số android: layout_column và android: layout_row. Những người không hiển thị trong các thuộc tính của prmsLinLyt. – Srikanth

7

Dưới đây là làm thế nào Bạn có thể tạo ra nó lập trình:

GridLayout gridLayout = new GridLayout(getContext()); 

// Add Title 
GridLayout.Spec titleTxtSpecColumn = GridLayout.spec(2, GridLayout.BASELINE); 
GridLayout.Spec titleRowSpec = GridLayout.spec(0); 
TextView titleText = new TextView(getContext()); 
titleText.setText("Title"); 

gridLayout.addView(titleText, new GridLayout.LayoutParams(titleRowSpec , titleTxtSpecColumn)); 

Đối biết thêm chi tiết xem ví dụ về ApiDemos.

+1

Tất cả các hàng/cột được thêm vào cùng một ô. – Softlion

+0

Thực ra bạn đã đúng – Softlion

Các vấn đề liên quan