I have this main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="@drawable/ic_launcher"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="@android:color/white"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:background="@drawable/barslide"
android:layout_marginLeft="500dp"/>
</LinearLayout>
</FrameLayout>
I want to create the same UI like in main.xml in java code. I try to code it but it doesn’t work, it’s not same as the xml. Here is the code:
void createUI(){
LayoutParams params1 = new FrameLayout
(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
LayoutParams params2 = new android.widget.FrameLayout.LayoutParams(
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
android.widget.FrameLayout.LayoutParams.FILL_PARENT,
Gravity.BOTTOM);
FrameLayout f1 = new FrameLayout(this);
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
LinearLayout l2 = new LinearLayout(this);
l2.setOrientation(LinearLayout.HORIZONTAL);
l2.setLayoutParams(params2);
view1 = new page1(getBaseContext());
view360 = view1.img1(getBaseContext());
view360.setBackgroundDrawable(getResources().getDrawable(R.drawable.black1));
view360.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
slidebar = view1.img3(getBaseContext());
slidebar.setBackgroundResource(R.drawable.barslide);
slidebar.setLayoutParams(new LinearLayout.LayoutParams(300, LayoutParams.WRAP_CONTENT,Gravity.BOTTOM));
f1.addView(view360);
f1.addView(l2);
l2.addView(slidebar);
addContentView(f1, params1);
}
Your code doesn’t quite replicate the
xmllayout(this was to big for a comment):You’ll have to modify it to use your object
page1(I don’t know exactly what are you trying to do there).Also, are you sure you have to use the
addContentView()(maybesetContentView?) method?(This method will add the views to the already present layout)