开发者

When Dynamically creating controls how to access them if no. of controls is not known

I created a compound control with a textview and 3 buttons. It works fine when i add it to a layout using xml. But it need to add it using java code at runtime. When i try to add it using java code the other controls are visible but my compound control is not shown.

COmpound Control XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/border_lines"

>
    <TextView android:开发者_JAVA百科id="@+id/msg_title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="SAMPLE MESSAGE TITLE"
    />
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
    >
        <Button android:id="@+id/btn_shw"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text="SHOW MSG"
            android:layout_weight="1"
        />
        <Button android:id="@+id/btn_dis"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text=" DISABLE"
            android:layout_weight="1"
        />
        <Button android:id="@+id/btn_del"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:text=" DELETE "
            android:layout_weight="1"
        />
    </LinearLayout>    
</LinearLayout>

JAVA CODE USED TO ADD THE CONTROLS

package deepak.android.remainder;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class RemainderList extends Activity 
{
    ScrollView sv1;
    LinearLayout ll1;
    deepak.android.remainder.RemainderControl rc1;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        sv1=new ScrollView(this);
        ll1=new LinearLayout(this);
        ll1.setOrientation(LinearLayout.VERTICAL);
        sv1.addView(ll1);

        TextView tv1=new TextView(this);
        tv1.setText("THIS IS SAMPLE TEXT");
        ll1.addView(tv1);

            **LinearLayout.LayoutParams lp;
            lp=new LineararLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);** 

        for(int i=0;i<5;i++)
        {
            rc1=new deepak.android.remainder.RemainderControl(this);
            **ll1.addView(rc1,lp);**

        }

        setContentView(sv1);
    }
}


You can do by querying for specific (numbered) children of ll1 (the LinearLayout). For example,

View child1 = ll1.getChildAt(0);
View child2 = ll1.getChildAt(1);
// and so on...

Of course, you will need to do a null check and a instanceof check, before you can cast and use the children.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜