Oll*_*lle 20 android android-layout
我正在尝试将动态内容添加到使用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 = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView 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(v);
Run Code Online (Sandbox Code Playgroud)
和要匹配的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">
<ScrollView
android:id="@+id/scrollView1"
android:clickable="true"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_marginBottom="50px"
android:layout_height="fill_parent">
</ScrollView>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
希望这会对某人有所帮助.谢谢大家的帮助!
您向视图添加内容的方式基本上是正确的 - 您只需通过其ID获取容器对象,然后添加它们.
看起来您将内容视图设置为错误的视图.您应该膨胀视图,添加子项并将其设置为内容视图,或者从资源ID设置内容视图然后执行操作.你正在做的是操纵一个视图,然后添加一个不同的视图.尝试将setContentView(...)移动到块的开头.
归档时间: |
|
查看次数: |
36883 次 |
最近记录: |