3

I am inflating a xml file to a custom view and adding it to a Relative Layout.

While adding the custom views to Relative Layout i am setting some rules using the addRule method. But the views are always getting added at top left corner, am i missing something here?

here is my code and layouts.

MainActivity.java

public class MainActivity extends Activity {

        @Override
        protected void onCreate(android.os.Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainLayout);
            RelativeLayout rl = (RelativeLayout)findViewById(R.id.relativeLayout);

            CustomLayout cLayout = new CustomLayout(this);
            RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.BELOW, R.id.title);
            cLayout.setLayoutParams(lp);
            rl.addView(cLayout);        
        };
    }

CustomLayout.java

public class CustomLayout extends RelativeLayout {

    public CustomLayout(Context context) {
        super(context);
        LayoutInflater.from(context).inflate(R.layout.customlayout, this);
    }
}

mainlayout.xml

<HorizontalScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contentScroller"
    android:layout_width="wrap_content"
    android:layout_height="match_parent">
    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <RelativeLayout
            android:id="@+id/mainInfo"
            android:layout_width="400dp"
            android:layout_height="match_parent" >

            <ImageView
                android:id="@+id/mainImage"
                android:layout_width="200dp"
                android:layout_height="200dp"
                android:src="@drawable/lines"/>
            <TextView 
                android:id="@+id/mainDesc" 
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/mainImage"
                android:textColor="@android:color/white"
                android:text="@string/desc"/>
        </RelativeLayout>
        <RelativeLayout 
            android:id="@+id/relativeLayout"
            android:layout_height="match_parent"
            android:layout_width="match_parent"
            android:layout_toRightOf="@+id/mainInfo">
            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" 
                android:text="@string/titletext"/>
       </RelativeLayout>
    </RelativeLayout>
</HorizontalScrollView>

customlayout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/background">
<ImageView
    android:id="@+id/icon"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginRight="10dp"
    android:scaleType="fitStart"
    android:src="@drawable/appicon" >
</ImageView>
<TextView
    android:id="@+id/name"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/sampletext"
    android:layout_toRightOf="@+id/icon"/>
</RelativeLayout>
Mohan Krishna
  • 352
  • 3
  • 15
  • Your code should work. Is that all the layout for `mainlayout.xml`? If not post the **full** version. – user Nov 08 '12 at 11:16
  • All seems ok. I've even tested your current layouts and code but I don't see the behavior you talk about(at least on a phone with 2.2). Are you sure you don't do something else? – user Nov 08 '12 at 12:45

0 Answers0