Frage

I am having a relative layout I want to create the layout dynamically the layout in XMl lookes like

<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" >

I made this like -

RelativeLayout.LayoutParams lprams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        lprams.setMargins(5,5,5,5);

But how to add the background I am doing this in a Fragment not in a activity

War es hilfreich?

Lösung

try as follows...

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

Andere Tipps

Try This : -

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

You can use setBackground for that.

Drawable bg_image= getResources().getDrawable(R.drawable.image); 
myLayout.setBackground(bg_image);

Hope it helps.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top