android - 以编程方式将边距添加到 RelativeLayout

标签 android android-layout

我正在尝试在我的 Activity 中以编程方式将边距顶部添加到我的 RelativeLayout 中。使用 xml 我可以在这种模式下执行此操作:android:layout_marginTop="10dp",但是当我尝试以编程方式执行此操作时,没有任何变化...如您所见,我正在使用一些RelativeLayout(有一个 for 循环)在一个 LinearLayout 容器中。

这是我正在使用的代码:

//LINEAR LAYOUT
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT));

for (int i=1; i<=3; i++){
    //RELATIVE LAYOUT
    RelativeLayout relativeLayout = new RelativeLayout(this);
    relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
            RelativeLayout.LayoutParams.FILL_PARENT));
    relativeLayout.setBackgroundColor(getResources().getColor(R.color.grayColor));

    //CODE FOR ADD MARGINS
    RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
    relativeParams.topMargin=80;
    relativeLayout.setLayoutParams(relativeParams);

    //IMAGE VIEW
    ImageView selectedPhoto = new ImageView(this);
    selectedPhoto.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    selectedPhoto.setImageResource(R.drawable.ic_launcher);

    //TEXT VIEWS
    TextView numberCopies = new TextView(this);
    numberCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    numberCopies.setGravity(Gravity.CENTER);
    numberCopies.setPadding(25, 25, 25, 25);
    numberCopies.setTextColor(getResources().getColor(R.color.blackColor));
    numberCopies.setText("2 copies ");
    RelativeLayout.LayoutParams layoutParamsNumberCopies = (RelativeLayout.LayoutParams) numberCopies.getLayoutParams();
    layoutParamsNumberCopies.addRule(RelativeLayout.CENTER_HORIZONTAL);
    numberCopies.setLayoutParams(layoutParamsNumberCopies);

    TextView priceCopies = new TextView(this);
    priceCopies.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
    priceCopies.setGravity(Gravity.CENTER);
    numberCopies.setPadding(25, 25, 25, 25);
    priceCopies.setTextColor(getResources().getColor(R.color.redColor));
    priceCopies.setText("$ 25 ");
    RelativeLayout.LayoutParams layoutParamsPriceCopies = (RelativeLayout.LayoutParams) priceCopies.getLayoutParams();
    layoutParamsPriceCopies.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    priceCopies.setLayoutParams(layoutParamsPriceCopies);

    relativeLayout.addView(selectedPhoto);
    relativeLayout.addView(numberCopies);
    relativeLayout.addView(priceCopies);
    linearLayout.addView(relativeLayout);
}
scrollView.addView(linearLayout);
setContentView(scrollView);

我认为失败的 block 代码是这样的:

//CODE FOR ADD MARGINS
RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.topMargin=80;
relativeLayout.setLayoutParams(relativeParams);

最佳答案

找到解决办法。

当你在 LinearLayout 中使用 RelativeLayout 时,你需要使用 LinearLayout.LayoutParams 而不是 RelativeLayout.LayoutParams.

所以替换你下面的代码...

RelativeLayout.LayoutParams relativeParams = (RelativeLayout.LayoutParams)relativeLayout.getLayoutParams();
relativeParams.setMargins(0, 80, 0, 0);  // left, top, right, bottom
relativeLayout.setLayoutParams(relativeParams);

与...

// CODE FOR ADD MARGINS
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
        new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
linearParams.setMargins(0, 80, 0, 0);
relativeLayout.setLayoutParams(linearParams);
relativeLayout.requestLayout();

关于android - 以编程方式将边距添加到 RelativeLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18844418/

相关文章:

android - 当滚动位置位于顶部时,SwipeRefreshLayout + WebView

Android 深度链接 - 返回堆栈

android - 错误在启动AVD时意味着什么

android - 弹出菜单从操作栏中的图标展开/折叠

android - ExpandableListView.addHeaderView() 没有保留布局参数

android - Material 设计 - 操作栏标题和内容的左边距不匹配

android - AlertDialog 未显示

Android ImageButton按下时缩小

android - 将 DrawerLayout 与 CoordinatorLayout 和 FAB 相结合

android - 固定屏幕布局比例