我有一个相对布局我想动态创建布局,XMl中的布局看起来像
<RelativeLayout
android:id="@+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
android:background="@drawable/step_background"
android:orientation="vertical" >
我这样做 -
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
但是如何添加背景我在片段而非活动中这样做
答案 0 :(得分:1)
尝试如下......
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
lprams.setMargins(5,5,5,5);
layout.setLayoutParams(lprams);
layout.setBackgroundResource(R.drawable.step_background);
答案 1 :(得分:0)
试试这个: -
RelativeLayout mRelativeLayoutPopup = new RelativeLayout(context);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
layoutParams.setMargins(5,5,5,5);
mRelativeLayoutPopup.setLayoutParams(layoutParams);
mRelativeLayoutPopup.setBackgroundResource(drawable_background);
答案 2 :(得分:0)
您可以使用setBackground
。
Drawable bg_image= getResources().getDrawable(R.drawable.image);
myLayout.setBackground(bg_image);
希望它有所帮助。