2013-04-02 35 views
4

我目前正在以編程方式將RelativeLayout封裝在一個Linearlayout中。該基地是一個滾動視圖,我想這些佈局添加到名爲滾動型svbaseSetMargin以編程方式無法正常工作RelativeLayoutParams

LinearLayout llbase = new LinearLayout(getApplicationContext()); 
LinearLayout.LayoutParams llbaseParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose! 
llbaseParams.setMargins(0, new CommonOpearation().getPixel(10, getApplicationContext()), 0, new CommonOpearation().getPixel(10, getApplicationContext())); 

llbase.setLayoutParams(llbaseParams); 
llbase.setGravity(Gravity.CENTER_HORIZONTAL); 

for(int n =0;n<2;n++) 
{ 

    RelativeLayout rLayoutBase = new RelativeLayout(getApplicationContext()); 
    RelativeLayout.LayoutParams rLayoutParms = new RelativeLayout.LayoutParams(new CommonOpearation().getPixel(140, getApplicationContext()), new CommonOpearation().getPixel(125, getApplicationContext())); 
    **rLayoutParms.setMargins(0, 0, new CommonOpearation().getPixel(5, getApplicationContext()), 0);** 
    rLayoutBase.setLayoutParams(rLayoutParms); 


    Drawable drawable = MyCurrentActivity.this.getApplicationContext().getResources().getDrawable(R.drawable.curved_bg); //new Image that was added to the res folder 
    try { 
     rLayoutBase.getClass().getMethod(android.os.Build.VERSION.SDK_INT >= 16 ? "setBackground" : "setBackgroundDrawable", Drawable.class).invoke(rLayoutBase, drawable); 
    } catch (Exception ex) { 

    } 

    llbase.addView(rLayoutBase); 
} 


    svBase.addView(llbase); 

正如你可以看到我有兩個RelativeLayout的封裝在用的LinearLayout水平方向。我已經嘗試使用setMargin爲每個相關佈局賦予一定的5dp權限。然而,它並沒有在兩個相關佈局之間給出保證金。它工作,如果我要在手動在XML中做,但。

可以在圖像上看到不同之處。頂部,而底部的兩個RelativeLayout的生成編程

enter image description here

回答

相關問題