2015-11-21 96 views
0
GridLayout glSameLoactions; 
@OnClick(R.id.tvAddLocation) 
void addLocationOnClickListener(){ 
    if(i==0) { 
     glSameLoactions = new GridLayout(this); 
     glSameLoactions.setColumnCount(3); 
     glSameLoactions.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled); 
     glSameLoactions.setPadding(16,16,16,16); 
    } 
    RelativeLayout rlLocation = new RelativeLayout(this); 
    rlLocation.setPadding(16, 16, 16, 16); 
    LinearLayout.LayoutParams relativeParams = new LinearLayout.LayoutParams(300,200); 
    relativeParams.setMargins(16, 16, 16, 16);//<<<<----- NOT WORKING 
    rlLocation.setLayoutParams(relativeParams); 
    rlLocation.requestLayout(); 
    rlLocation.setBackgroundResource(R.drawable.shape_rounded_corner_blurfilled); 

    TextView tvLocationName = new TextView(this); 
    tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT)); 
    tvLocationName.setId(R.id.tvLocationName); 
    tvLocationName.setText("amritsar,PB"); 
    tvLocationName.setTextColor(getResources().getColor(R.color.white)); 
    tvLocationName.setTextSize(14f); 

    ImageView ivRadiobtn = new ImageView(this); 
    tvLocationName.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 
      ViewGroup.LayoutParams.WRAP_CONTENT)); 
    ivRadiobtn.setImageResource(R.drawable.ic_circle_empty); 
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
      RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 
    lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
    rlLocation.addView(tvLocationName); 
    rlLocation.addView(ivRadiobtn,lp); 
    glSameLoactions.addView(rlLocation); 
if (i==0) { 
llLocations.addView(glSameLoactions); 
llLocations.setVisibility(View.VISIBLE); 
} 
    i++; 

} 

我正在編程佈局,其中點擊按鈕,在網格佈局中添加一個新的相對佈局。我所有的代碼工作正常,但問題是我無法將邊距設置爲網格佈局內的相對佈局。請幫忙!!setMargins相對佈局不工作在網格佈局

感謝

回答

0

您應該使用RelativeLayout.LayoutParams而不是LinearLayout.LayoutParams

試試這個:

RelativeLayout.LayoutParams relativeParams =new RelativeLayout.LayoutParams(300, 300); 
relativeParams.setMargins(16, 16, 16, 16); 
rlLocation.setLayoutParams(relativeParams); 
+0

已經嘗試過。但不工作 –