Postnik
Postnik

Reputation: 21

Linearlayout overlay another linearlayout

I have some problem with add linearlayout dynamically. It's add on the top of screen, overlay other linearlayout.

Here XML,code and results.

XML:

<TextView
    android:id="@+id/top_km"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:background="#888"
    android:gravity="top"
    android:textColor="#fff"
    android:textSize="30dip"
    android:layout_centerHorizontal="true"/>

<RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@+id/top_km"
        android:id="@id/textLayout">

</RelativeLayout>

Code:

myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);  
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);

Results: enter link description here

Thanks

Upvotes: 0

Views: 4533

Answers (3)

Ken Wolf
Ken Wolf

Reputation: 23279

Don't use a RelativeLayout as your holder. Use a LinearLayout with orientation="vertical" instead.

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/top_km"
    android:orientation="vertical"
    android:id="@id/textLayout" />

then in code

myLayout = (LinearLayout) page.findViewById(R.id.textLayout);

followed by

// rest of your code

Upvotes: 2

Guillermo Merino
Guillermo Merino

Reputation: 3257

Maybe it's easier for you to add it in the XML file with android:visibility="GONE" and then in the code just show it (View.VISIBLE) or hide it (View.GONE).

Upvotes: 0

Dmitry Nelepov
Dmitry Nelepov

Reputation: 7306

It's because you use RealativeLayout for proper adding use 1. RelativeLayout.LayoutParams for st LayoutParams 2. In LayoutParams use field below

Example:

RelativeLayout rl=new RelativeLayout(this);
LinearLayout ll1=new LinearLayout(this);
TextView tx1=new TextView(this);
tx1.setText("Test1");
ll1.addView(tx1);
rl.addView(ll1);
LinearLayout ll2=new LinearLayout(this);
TextView tx2=new TextView(this);
tx2.setText("Test1");
ll2.addView(tx1);
rl.addView(ll2);
RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();

And then use lp2.addRule

Here some help:

Parameters verb One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT. anchor The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.

Upvotes: 1

Related Questions