开发者

Dynamical creation of UI

开发者 https://www.devze.com 2023-02-26 10:41 出处:网络
I want to implement the following layout in code: <RelativeLayout xmlns:android=\"http://sch开发者_StackOverflowemas.android.com/apk/res/android\"

I want to implement the following layout in code:

<RelativeLayout xmlns:android="http://sch开发者_StackOverflowemas.android.com/apk/res/android"
     android:id="@+id/widget92"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >

     <Button
          android:id="@+id/widget164"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_alignTop="@+id/widget163"
          android:layout_alignLeft="@+id/widget161" />

     <Button
          android:id="@+id/widget163"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_alignTop="@+id/widget162"
          android:layout_toLeftOf="@+id/widget161" />

     <Button
          android:id="@+id/widget162"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_below="@+id/widget161"
          android:layout_toLeftOf="@+id/widget160" />

    <Button
          android:id="@+id/widget161"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/widget160" />

    <Button
          android:id="@+id/widget160"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/widget159" />

    <Button
          android:id="@+id/widget159"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Button"
          android:layout_alignParentTop="true"
          android:layout_alignParentLeft="true" />

</RelativeLayout>

Can anyone tell me how to implement this in code. Thanx in advance.


You can create all UI elements manually with the same hierarchy as in layout xml file.

Say in Activity.onCreate()

onCreate(...) {
    super.onCreate(...);
    RelativeLayout layout = new RelativeLayout(...);
    Button button = new Button(...);
    layout.addView(button);
    setContentView(layout);
}


RelativeLayout rl = new RelativeLayout(this);
        LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
        rl.setId(R.id.widget92);

        Button btn = new Button(this);
        btn.setId(R.id.widget164);
        btn.setText("Text");
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.ALIGN_TOP, R.id.widget163);
        lp.addRule(RelativeLayout.ALIGN_LEFT, R.id.widget161);
        rl.addView(btn, lp);

        btn = new Button(this);
        lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ...

        setContentView(rl);
0

精彩评论

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