创建和设置保证金纲领性的相对布局Android版 [英] create and set margin programmatic for relative layout android
问题描述
您好我正在开发中,我创造相对布局规划和试图建立保证金为并将其添加成具有定向线性线性布局android应用。
因此,这里是我的code:
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. 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.
推荐答案
该类型的LayoutParams您使用视图实际上应该从其父。
The LayoutParams type you use on a view should actually be from its parent.
因此,如果您要添加的RelativeLayout到的LinearLayout,你设置你的RelativeLayout中的LayoutParams实际上应该是一个LinearLayout.LayourParams,而不是RelativeLayout.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.
这篇关于创建和设置保证金纲领性的相对布局Android版的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!