2016-04-01 69 views
2

我想以線性佈局顯示動態數據,但問題是我有4列,其中第1列標題是靜態的,其餘3是動態的。知道如何設置標題動態我想知道的是如何在單一佈局中設置靜態和動態。 我的代碼如何以編程方式在單個線性佈局中設置靜態和動態文本

private void addSizeGuideTable (ProductSizingDetail productSizingDetail){ 
    for (int i = 0; i < productSizingDetail.getSizeGuideList().size(); i++) { 
     SizeGuides sizeGuides = productSizingDetail.getSizeGuideList().get(i); 
     if (i == 0) { 
      addColumn(sizeGuides, true, false); 
     } else if (i == productSizingDetail.getSizeGuideList().size() - 1) { 
      addColumn(sizeGuides, false, true); 
     } else { 
      addColumn(sizeGuides, true, true); 
     } 
    } 
} 

private void addColumn(SizeGuides sizeGuides, boolean rightBorder, boolean leftBorder) { 
    LinearLayout linearLayout = new LinearLayout(this); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 
    linearLayout.setBackgroundColor(Color.parseColor("#d8d8d8")); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1); 
    linearLayout.setLayoutParams(params); 
    if (rightBorder && leftBorder) { 
     linearLayout.setPadding(1, 0, 1, 0); 
    } else if (rightBorder && !leftBorder) { 
     linearLayout.setPadding(0, 0, 1, 0); 
    } else { 
     linearLayout.setPadding(1, 0, 0, 0); 
    } 

    addHeaders(sizeGuides, linearLayout); 
    addRow(sizeGuides, linearLayout); 

    ((LinearLayout) findViewById(R.id.layout_size_guide)).addView(linearLayout); 
} 

private void addHeaders(SizeGuides sizeGuides, LinearLayout linearLayout) { 
    TextView tv = new TextView(this); 
    tv.setText(sizeGuides.getDimension()); 
    tv.setBackgroundColor(Color.parseColor("#f8f8f8")); 
    tv.setSingleLine(); 
    tv.setGravity(Gravity.CENTER); 
    tv.setPadding(0, 10, 0, 10); 
    tv.setTypeface(TypeFaceProvider.heavy(this)); 
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
    params.setMargins(0, 0, 0, 1); 
    tv.setLayoutParams(params); 
    linearLayout.addView(tv); 
} 

private void addRow(SizeGuides sizeGuides, LinearLayout linearLayout) { 
    for (int i = 0; i < sizeGuides.getDimensionList().size(); i++) { 
     DimensionItem dimension = sizeGuides.getDimensionList().get(i); 
     TextView tv = new TextView(this); 
     if(dimension.getDimensionUnit().equalsIgnoreCase("INCH")) { 
      tv.setText(dimension.getDimensionValue()+"\" "); 
     }else{ 
      tv.setText(dimension.getDimensionValue()); 
     } 
     tv.setBackgroundColor(Color.parseColor("#f8f8f8")); 
     tv.setSingleLine(); 
     tv.setGravity(Gravity.CENTER); 
     tv.setPadding(0, 10, 0, 10); 
     tv.setTypeface(TypeFaceProvider.light(this)); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
     if (i == sizeGuides.getDimensionList().size() - 1) { 
      params.setMargins(0, 1, 0, 0); 
     } else { 
      params.setMargins(0, 1, 0, 1); 
     } 
     tv.setLayoutParams(params); 
     linearLayout.addView(tv); 
    } 
} 
+0

當你說第一列標題是靜態的,你的意思是你不希望大小改變?你希望整個列是靜態的?或只是寬度? –

回答

0

您可以使用計數來添加標題。

private static headerSetupCounter =0; 

在你在你的 addSizeGuideTable在您檢查列private void addColumn(SizeGuides sizeGuides, boolean rightBorder, boolean leftBorder)

if(headerSetupCounter == 0) 
    addHeaders(YourCustomSizeGuide, linearLayout); 
else 
    addHeaders(sizeGuides, linearLayout); 

headerSetupCounter++; 

你也可以實現自定義大小(或靜態的,你提到它)。

if (i == 0) { 
    // addColumn(sizeGuides, true, false); 
    // Implement customised "static" column. 
相關問題