开发者

How to add components to current view in android

I am new to android and just trying to modify a basic example.

main.xml is like

<?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:weightSum="1">
<Button android:layout_width="228dp" android:layout_height="wrap_content" android:id="@+id/addButton" android:text="@string/addBtn"></Button>
 </LinearLayout>

And want to add TextField on Click of the button. How to add Child to view?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    btn=(But开发者_Python百科ton)findViewById(R.id.addButton);
    btn.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    EditText editText = new EditText(v.getContext());
    //Append the editText to View

}

How to append it to current view/layout?


Something like this would help you i think.

ParentView.post(new Runnable() {
                    public void run() {
                        ParentView.addView(newView,0);
                        LinearLayout.LayoutParams rlparams = (LinearLayout.LayoutParams)rldeal.getLayoutParams();
                        rlparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
                        rlparams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
                        newView.setLayoutParams(rlparams);
                    }
                });


Try to use include and merge:

http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜