2012-07-02 58 views
1

我試圖以編程方式在tablerow中設置textview和複選框的權重。經過一番研究後,我發現LayoutParams的第三個參數被用來做這件事。但是,它似乎並沒有工作。有人可以看看這段代碼,看看有什麼問題嗎?謝謝。 (使用LinearLayout建議編輯)設置顯示元素的重量

public LayerSelectorView(Context context) super(context); this.context = context; float density = context.getResources()。getDisplayMetrics()。density;

linearLayout = new LinearLayout(context); 
    linearLayout.setOrientation(LinearLayout.VERTICAL); 
    final LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    linearLayout.setLayoutParams(mainParams); 

    titleText = new TextView(context); 
    titleText.setTextAppearance(context, android.R.style.TextAppearance_Large); 
    titleText.setText("Select Layers"); 
    titleText.setTextColor(Color.WHITE); 
    titleText.setId(id); 

    final LinearLayout.LayoutParams titleParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT); 
    titleParams.setMargins(20, 20, 0, 0); 
    titleText.setLayoutParams(titleParams); 

    linearLayout.addView(titleText); 
} 
public void updateView() 
{ 

    overlays = ((MapActivity)context).getMapView().getOverlays(); 

    loadLayers(); 

    this.addView(linearLayout); 

    linearLayout.setBackgroundColor(0x88000000); 
    linearLayout.invalidate(); 
    this.invalidate(); 
} 
public void loadLayers() 
{ 
    TableLayout table = new TableLayout(context); 
    final LinearLayout.LayoutParams tableParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); 
    table.setLayoutParams(tableParams); 
    int i = id+1; 
    for(final MapOverlay mo: overlays) 
    {  
     TableRow tblRow = new TableRow(context); 

     TextView layer = new TextView(context); 
     TableRow.LayoutParams trParams = new TableRow.LayoutParams(0, LayoutParams.FILL_PARENT, 1f); 
     layer.setTextAppearance(context, android.R.style.TextAppearance_Large); 
     layer.setText(mo.getName()); 
     layer.setTextColor(Color.WHITE); 
     layer.setId(i); 
     i++; 
     layer.setLayoutParams(trParams); 

     final CheckBox chk = new CheckBox(context); 
     TableRow.LayoutParams trParams1 = new TableRow.LayoutParams(0, LayoutParams.FILL_PARENT, .2f); 
     chk.setLayoutParams(trParams1); 
     chk.setFocusable(false); 
     chk.setClickable(false); 
     chk.setChecked(true); 

     tblRow.addView(layer); 
     tblRow.addView(chk); 
     table.addView(tblRow); 

     tblRow.setOnClickListener(new OnClickListener(){ 

      public void onClick(View v) { 
       if(mo.isVisible()) 
       { 
        mo.setVisible(false); 
        chk.setChecked(false); 
       } 
       else 
       { 
        mo.setVisible(true); 
        chk.setChecked(true); 
       } 
      } 

     }); 
    } 
    linearLayout.addView(table); 
} 
+0

使用LinearLayout而不是RelativeLayout –

回答

0

您不能在RelativeLayout中使用權重。你需要使用LinearLayout。我不知道這是否會對你有用,但我希望它能幫到你。

+0

謝謝,我相信這將適用於重量功能,任何想法這個新的設置與linearlayout版本不使用整個寬度的父視圖? (編輯的主塊) – user1496741

+0

使用wrap_content方法與fill_parent – BlackHatSamurai

+0

這應該會給你所需的所有信息:http://developer.android.com/reference/android/widget/LinearLayout.LayoutParams.html – BlackHatSamurai

相關問題