2015-07-01 53 views
-1

我不知道如何使這個,請幫助如何使Android佈局像標記表一樣?

我想在Android的XML格式的markshit佈局視圖。 我怎麼能做到,我可以用ListView控件,使學期動態..

  sem 1 sem 2 sem 3.... Total 
math   20  25  22  67 
science  18  22  24  64 
english  18  22  24  64 
physical  20  25  22  67 

Total   76  94  92  262 
+0

使用tablelayoute –

+0

是,使用列表視圖它。 – SANAT

+0

你是否從webservice獲取數據爲JSON? –

回答

0

在這種佈局中,使用兩個表格佈局IAM。一個用於標題,另一個用於數據。數據被包裝在滾動視圖中,因此標題將被固定並且數據可以被滾動。另外,我還有一個隱藏的行,每個行都有一個隱藏的行,其中存儲了很長的headertext或datatext。通過這樣做,我們可以使列平均填充。否則我們可能會以錯誤的數據表示形式出現。

public class MainActivity extends AppCompatActivity { 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.mark_sheet); 

      try { 
       DisplayMarkSheet(new JSONObject(ReturnJsonData())); 
      } catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     String ReturnJsonData() { 

      return "{\"SemisterData\":" + 
      //Specify your columns here 
      "{\"Columns\":" + 
       "[" + 
        "{\"ColumnName\":\"\"}," + //This column is intentionally blank. First Row, First Column will be blank 
        "{\"ColumnName\":\"Semister1\"}," + 
        "{\"ColumnName\":\"Semister2\"}," + 
        "{\"ColumnName\":\"Semister3\"}," + 
        "{\"ColumnName\":\"Semister4\"}," + 
        "{\"ColumnName\":\"Semister5\"}]," + 
      "\"FooterRows\":" + 
       "[" + 
        "{\"Cells\":" + 
         "[" + 
          "{\"CellValue\":\"Total\"}," + 
          "{\"CellValue\":\"160\"}," + 
          "{\"CellValue\":\"200\"}," + 
          "{\"CellValue\":\"240\"}," + 
          "{\"CellValue\":\"300\"}," + 
          "{\"CellValue\":\"340\"}]}]," + 
      "\"Rows\":" + //One row for each subject, Rows will have cells with data 
       "[" + 
        "{\"Cells\":" + 
         "[" + 
          "{\"CellValue\":\"Maths\"}," + 
          "{\"CellValue\":\"40\"}," + 
          "{\"CellValue\":\"50\"}," + 
          "{\"CellValue\":\"60\"}," + 
          "{\"CellValue\":\"70\"}," + 
          "{\"CellValue\":\"80\"}]}," + 

        "{\"Cells\":" + 
         "[" + 
          "{\"CellValue\":\"Science\"}," + 
          "{\"CellValue\":\"40\"}," + 
          "{\"CellValue\":\"50\"}," + 
          "{\"CellValue\":\"60\"}," + 
          "{\"CellValue\":\"70\"}," + 
          "{\"CellValue\":\"80\"}]}," + 
        "{\"Cells\":" + 
         "[" + 
          "{\"CellValue\":\"English\"}," + 
          "{\"CellValue\":\"40\"}," + 
          "{\"CellValue\":\"50\"}," + 
          "{\"CellValue\":\"60\"}," + 
          "{\"CellValue\":\"70\"}," + 
          "{\"CellValue\":\"80\"}]}," + 
        "{\"Cells\":" + 
         "[" + 
          "{\"CellValue\":\"Physics\"}," + 
          "{\"CellValue\":\"40\"}," + 
          "{\"CellValue\":\"50\"}," + 
          "{\"CellValue\":\"60\"}," + 
          "{\"CellValue\":\"70\"}," + 
          "{\"CellValue\":\"80\"}]}]}}"; 
     } 

     public TextView GetHeaderTextView(String HeaderText) { 
      TextView title = new TextView(this); 
      title.setText(HeaderText); 
      title.setGravity(Gravity.CENTER); 
      title.setTextColor(Color.BLACK); 
      title.setTypeface(Typeface.DEFAULT_BOLD); 
      title.setBackgroundColor(Color.WHITE); 
      title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      title.setPadding(10, 10, 10, 10); 
      return title; 
     } 

     public TextView GetHeaderTextViewHidden(String HeaderText) { 
      TextView title = new TextView(this); 
      title.setText(HeaderText); 
      title.setGravity(Gravity.CENTER); 
      title.setBackgroundColor(Color.parseColor("#A9A9A9")); 
      title.setTextColor(Color.BLACK); 
      // title.setTextAppearance(context, color.RowText); 
      title.setTypeface(Typeface.DEFAULT_BOLD); 
      title.setHeight(0); 
      title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      title.setPadding(10, 10, 10, 10); 
      return title; 
     } 

     public TextView GetItemTextView(String ItemText, String ColumnAlign) { 
      TextView text = new TextView(this); 
      text.setText(ItemText); 

      if (ColumnAlign.equals("L")) 
       text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); 
      else if (ColumnAlign.equals("R")) 
       text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
      else if (ColumnAlign.equals("C")) 
       text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); 
      text.setPadding(10, 10, 10, 10); 
      text.setTypeface(Typeface.DEFAULT_BOLD); 
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      // text.setCompoundDrawablesWithIntrinsicBounds(0, 0, 
      // R.drawable.next_image, 0); 
      text.setTextColor(Color.BLACK); 

      return text; 
     } 

     public static TextView GetItemTextViewHidden(Context context, String ItemText, String ColumnAlign) { 
      TextView text = new TextView(context); 
      text.setText(ItemText); 
      if (ColumnAlign.equals("L")) 
       text.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL); 
      else if (ColumnAlign.equals("R")) 
       text.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL); 
      else if (ColumnAlign.equals("C")) 
       text.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL); 
      text.setPadding(10, 10, 10, 10); 
      text.setTypeface(Typeface.DEFAULT_BOLD); 
      text.setHeight(0); 
      text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15); 
      text.setTextColor(Color.WHITE); 
      return text; 
     } 

     public void DisplayMarkSheet(JSONObject json) throws JSONException { 
      TableLayout headerTable = (TableLayout) findViewById(R.id.header); 
      TableLayout dataTable = (TableLayout) findViewById(R.id.maintable); 

      headerTable.setStretchAllColumns(true); 
      dataTable.setStretchAllColumns(true); 

      TableRow headerRow = new TableRow(this); 
      TableRow invisibleHeaderRow = new TableRow(this); 

      JSONArray columnsArray = json.getJSONObject("SemisterData").getJSONArray("Columns"); 
      JSONArray rowsArray = json.getJSONObject("SemisterData").getJSONArray("Rows"); 
      JSONArray footerArray = json.getJSONObject("SemisterData").getJSONArray("FooterRows"); 

      // Create header row and add Columns 
      if (json.getJSONObject("SemisterData").isNull("Columns") == false) { 
       headerRow.setBackgroundColor(Color.WHITE); 
       for (int i = 0; i <= columnsArray.length() - 1; i++) { 
        headerRow.addView(GetHeaderTextView(columnsArray.getJSONObject(i).getString("ColumnName"))); 
        invisibleHeaderRow.addView(GetHeaderTextViewHidden(columnsArray.getJSONObject(i).getString("ColumnName"))); 
       } 
       headerTable.addView(headerRow); 
       headerTable.addView(invisibleHeaderRow); 
      } 

      // Create data row and add data 
      String headerText = ""; 
      String rowText; 
      if (json.getJSONObject("SemisterData").isNull("Rows") == false) { 
       for (int i = 0; i <= rowsArray.length() - 1; i++) { 
        TableRow dataRow = new TableRow(this); 
        TableRow invisibledataRow = new TableRow(this); 

        dataRow.setBackgroundColor(Color.WHITE); 

        int cellsLength = 0; 
        if (rowsArray.getJSONObject(i).isNull("Cells") == false) 

        { 
         cellsLength = rowsArray.getJSONObject(i).getJSONArray("Cells").length(); 
         for (int k = 0; k <= cellsLength - 1; k++) { 

          dataRow.addView(GetItemTextView(rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C")); 
          rowText = rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"); 

          headerText = (String) ((TextView) invisibleHeaderRow.getChildAt(k)).getText(); 
          if (headerText.length() > rowText.length()) 
           invisibledataRow.addView(GetItemTextViewHidden(this, headerText, "C")); 
          else { 
           invisibledataRow.addView(GetItemTextViewHidden(this, rowsArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C")); 
           ((TextView) invisibleHeaderRow.getChildAt(k)).setText(rowText); 
          } 

         } 
         dataRow.setPadding(0, 5, 0, 5); 
         dataTable.addView(dataRow); 
         dataTable.addView(invisibledataRow); 
        } 
       } 

      } 

      // Create footer row and populate data 
      if (json.getJSONObject("SemisterData").isNull("FooterRows") == false) { 
       for (int i = 0; i <= footerArray.length() - 1; i++) { 
        TableRow footerRow = new TableRow(this); 
        footerRow.setBackgroundColor(Color.WHITE); 
        if (footerArray.getJSONObject(i).isNull("Cells") == false) { 
         for (int k = 0; k <= footerArray.getJSONObject(i).getJSONArray("Cells").length() - 1; k++) { 
          footerRow.addView(GetItemTextView(footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue"), "C")); 
          footerRow.setPadding(0, 5, 0, 5); 

          if (headerText.length() < footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue").length()) 
           ((TextView) invisibleHeaderRow.getChildAt(k)).setText(footerArray.getJSONObject(i).getJSONArray("Cells").getJSONObject(k).getString("CellValue")); 
         } 
         dataTable.addView(footerRow); 
        } 
       } 
      } 
     } 
} 

XML文件

![<?xml version="1.0" encoding="utf-8"?> 
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/WidgetsHSV" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:fillViewport="true" > 

    <LinearLayout 
     android:id="@+id/GridLL" 
     android:layout_width="wrap_content" 
     android:layout_height="fill_parent" 
     android:orientation="vertical" > 

     <TableLayout 
      android:id="@+id/header" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:background="@android:color/black" > 
     </TableLayout> 

     <ScrollView 
      android:id="@+id/table_scroll" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:fillViewport="true" > 

      <TableLayout 
       android:id="@+id/maintable" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:background="@android:color/black" > 
      </TableLayout> 
     </ScrollView> 
    </LinearLayout> 

</HorizontalScrollView>] 

輸出 enter image description here

1

如果您正在使用GridLayout然後按一個空的視圖位置0您可以使用GridLayout這個或LinearLayout

和位置1,2,3,4,5 ...列將包含您學期的標題和最後一列指數將總

現在下一行的第一個元素將是你的主題標題和同爲每對

必須相應地安排你的模型,以獲得正確的數據表示

如果你正在使用LinearLlayout那麼你的佈局將使用LinearLayout weightSum XML和權衡財產

+0

接受答案,如果它可以幫助你。 –

相關問題