0

在此处输入图像描述

我是安卓新手。我已经在带有文本苹果的 .java 文件中动态添加了 textview,但我想将它添加到顶部。我怎样才能做到这一点。

这是我的代码......在 .jave 文件中

  TextView lblname;
  LinearLayout linearlayout;
  lblname = new TextView(this);
  linearlayout = (LinearLayout) findViewById(R.id. linearlayout);
  linearlayout.addView(lblname);

先感谢您..

4

3 回答 3

0

Simply specify the index where you want to add the child, as a second parameter for the addView.

For example, to add it at the top:

linearlayout.addView(lblname, 0);

See reference: http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, int)

于 2013-04-29T05:02:37.213 回答
0

Try this

lblname = new TextView(this);
lblname.setText("APPLE");
lblname.setId(5); // id should be unique
lblname.setLayoutParams(new LayoutParams(
        LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT));
linearlayout.addView(lblname,index);//specify your index
于 2013-04-29T05:03:49.403 回答
0

将最后一行修改为:-

linearlayout.addView(lblname, index); 

将 index 替换为您要添加视图的位置,即,如果在开头,则为 0;如果在布局中的最后一个视图之前,则为 linearlayout.getChildCount()-1

于 2013-04-29T05:06:58.587 回答