相关疑难解决方法(0)

如何动态地向使用XML创建的视图添加元素

我正在尝试将动态内容添加到使用XML创建的视图中.

我有一个视图"profile_list",它有一个我想添加一些元素的ScrollView.

这是我正在尝试做的一些代码.

// Find the ScrollView 
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);

// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);

// Add the LinearLayout element to the ScrollView
sv.addView(ll);

// Display the view
setContentView(R.layout.profile_list);
Run Code Online (Sandbox Code Playgroud)

计划是添加一个TableLayout并动态填充它,而不仅仅是一个虚拟文本,但首先我必须让它工作.

欢迎任何帮助.

亲切的问候Olle

我找到了解决方案!

愚蠢的我在我的XML文件中的ScrollView中留下了一个元素!

无论如何,她是一个有效的例子:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.profile_list, null);

    // Find the ScrollView 
    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);

    // Create a LinearLayout element
    LinearLayout ll = …
Run Code Online (Sandbox Code Playgroud)

android android-layout

20
推荐指数
1
解决办法
4万
查看次数

标签 统计

android ×1

android-layout ×1