Android 将页边距设置为相对布局在网格布局中不起作用 GridLayout-glSameLoactions; @OnClick(R.id.tvAddLocation) void addLocationOnClickListener(){ 如果(i==0){ GLSAMELOATIONS=新网格布局(此); glSameLoactions.setColumnCount(3); glSameLoactions.setBackgroundResource(R.drawable.shape\u rounded\u corner\u blurfilled); glSameLoactions.setPadding(16,16,16,16); } RelativeLayout rlLocation=新的RelativeLayout(此); rlLocation.setPadding(16,16,16,16); LinearLayout.LayoutParams relativeParams=新的LinearLayout.LayoutParams(300200); relativeParams.setMargins(16,16,16,16,16);//

Android 将页边距设置为相对布局在网格布局中不起作用 GridLayout-glSameLoactions; @OnClick(R.id.tvAddLocation) void addLocationOnClickListener(){ 如果(i==0){ GLSAMELOATIONS=新网格布局(此); glSameLoactions.setColumnCount(3); glSameLoactions.setBackgroundResource(R.drawable.shape\u rounded\u corner\u blurfilled); glSameLoactions.setPadding(16,16,16,16); } RelativeLayout rlLocation=新的RelativeLayout(此); rlLocation.setPadding(16,16,16,16); LinearLayout.LayoutParams relativeParams=新的LinearLayout.LayoutParams(300200); relativeParams.setMargins(16,16,16,16,16);//,android,Android,您应该使用RelativeLayout.LayoutParams而不是LinearLayout.LayoutParams 试试这个: GridLayout glSameLoactions; @OnClick(R.id.tvAddLocation) void addLocationOnClickListener(){ if(i==0) { glSameLoactions = new GridLayout(this); glSameLoactions.set

您应该使用
RelativeLayout.LayoutParams
而不是
LinearLayout.LayoutParams

试试这个:

 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++;

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