简体   繁体   English

为相对布局android创建并设置边距编程

[英]create and set margin programmatic for relative layout android

Hi I am developing android application in which I am creating relative layout programmatic and tried to set margin for that and Added it into linear layout which have orientation linear. 嗨,我正在开发android应用程序,其中我以编程方式创建相对布局,并尝试为此设置边距并将其添加到具有线性方向的线性布局中。 So here is my code: 所以这是我的代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/screen_background"
    tools:context=".ChooseChannelsFragment" >

    <LinearLayout 
      android:id="@+id/main_outer_llt"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="horizontal"
      >

  </LinearLayout> 

</RelativeLayout>

and inside fragment I am adding relative layout like this 在片段内部,我要像这样添加相对布局

RelativeLayout relativeLayout = new RelativeLayout(getActivity());
    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(200, 80);
    relativeParams.setMargins(20, 20, 20, 20);
    relativeLayout.setLayoutParams(relativeParams);
    relativeLayout.setBackgroundColor(getResources().getColor(R.color.green_color));
    linearLayout.addView(relativeLayout);

It create layout with given color and size but not accepting margins. 它使用给定的颜色和大小创建布局,但不接受边距。 Am I doing something wrong? 难道我做错了什么? How to do this? 这个怎么做? Need Help. 需要帮忙。 Thank you. 谢谢。

The LayoutParams type you use on a view should actually be from its parent. 您在视图上使用的LayoutParams类型实际上应该来自其父级。

So, if you're adding a RelativeLayout to a LinearLayout, the LayoutParams you set to your RelativeLayout should actually be a LinearLayout.LayourParams, and not a RelativeLayout.LayoutParams. 因此,如果要将RelativeLayout添加到LinearLayout,则设置为RelativeLayout的LayoutParams实际上应该是LinearLayout.LayourParams,而不是RelativeLayout.LayoutParams。

As others suggested, layout_margin# is the space between the parent's # edge and your view. 就像其他人建议的那样,layout_margin#是父级的#边缘和您的视图之间的空间。

  • # replaces "Left", "Right", "Top" or "Bottom" #替换“左”,“右”,“顶部”或“底部”

Getting/setting margins worked for me with: 获得/设置利润对我有用:

ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) mView.getLayoutParams();
params.topMargin += 20;
mView.requestLayout(); // important

Of course, my View was indeed a ViewGroup and the parent was a ViewGroup as well. 当然,我的View确实是一个ViewGroup,父级也是一个ViewGroup。 In most cases, you should cast your layout params to the parent's View class LayoutParams (in this case it's ViewGroup and RelativeLayout) 在大多数情况下,应将布局参数转换为父级的View类LayoutParams(在这种情况下为ViewGroup和RelativeLayout)

In this case, the father is LinearLayout. 在这种情况下,父亲是LinearLayout。 So you should use: 因此,您应该使用:

TableLayout.LayoutParams layoutParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT, TableLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(20, 20, 20, 20);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM