我已经看到这个问题添加到栈上,但信息还没有帮助或成功,所以我仍然不确定。动态添加新的窗口小部件控件到现有的布局
我想要做的要点:
我有一个在xml中定义的布局,例如some_details_view.xml。
setContentView(R.layout.some_details_view);
它具有利用一个父相对布局,线性头布局,线性页脚布局,包含容纳一些标签的相对布局的中间滚动布局布置文本视图一堆 - 值类型的文本视图。 并且在滚动视图相对布局的底部,我当前放置了一个框架布局作为占位符。
在创建相应的活动时,我在相应的文本视图中设置了文本,并使用之前活动移交的数据;基本的东西。
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<LinearLayout
android:id="@+id/header"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
...some header content
</LinearLayout>
<LinearLayout
android:id="@+id/footer"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
..some footer content
</LinearLayout>
<ScrollView
android:id="@+id/scroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/footer"
android:layout_below="@+id/header"
android:layout_margin="5dip" >
<RelativeLayout
android:layout_width="fill_parent"
android:id="@+id/relativeScroll"
android:layout_height="wrap_content" >
...text views in relative layout
<FrameLayout
android:id="@+id/placeholder"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/moreInfoValue" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
设置为给定的数据文本的意见后,我用一个异步任务中获得,我想表明作为静态形式布局底部的历史记录列表类型的东西一些额外的数据。可能有0个或更多项目,因此我要么添加1个或更多个文本视图,要么根本没有。
在post执行,我明白在主UI线程上运行,我尝试找到一个正在退出的容器布局/视图组,并添加一个新的线性布局,我添加新的文本视图,或者只是添加新文本直接查看现有容器布局。
这里是我试过的最新的东西:
ViewGroup mContainer = null; //defined as member variable of activity class and instatiated in on create
mContainer = (ViewGroup)findViewById(R.id.placeholder); //set in on create
LinearLayout ll = new LinearLayout(context); //on post execute of async task
ll.setOrientation(LinearLayout.VERTICAL);
mContainer.addView(ll); //add a new linear layout to an existing container layout
//add some new text view widgets items dynamically
for(NewDisplayItem item : items)
{
TextView tv = new TextView(context);
tv.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
tv.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
tv.setText(item.getSomeText());
ll.addView(tv); //add text view to new linear layout
}
当加载UI我没有看到新的项目,通过加强和看会添加控件使用上面的代码后添加到我的布局。
不确定它是什么,但除了它不工作的事实之外,这种方法似乎并不正确。当活动加载时,所有静态视图都会设置并在视图中显示。我弹出一个加载对话框,通过异步任务的东西,我想期待看到动态控件逐一添加到布局?
我想有一种方法,我可以膨胀一个新的布局在xml中定义每个新的DisplayItem,我想添加到我现有的布局....像这样,http://stackoverflow.com/questions/7977071/how-to-dynamically-add-a-layout – topwik