2012-08-31 35 views
1

我已经看到这个问题添加到栈上,但信息还没有帮助或成功,所以我仍然不确定。动态添加新的窗口小部件控件到现有的布局

我想要做的要点:
我有一个在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我没有看到新的项目,通过加强和看会添加控件使用上面的代码后添加到我的布局。

不确定它是什么,但除了它不工作的事实之外,这种方法似乎并不正确。当活动加载时,所有静态视图都会设置并在视图中显示。我弹出一个加载对话框,通过异步任务的东西,我想期待看到动态控件逐一添加到布局?

+0

我想有一种方法,我可以膨胀一个新的布局在xml中定义每个新的DisplayItem,我想添加到我现有的布局....像这样,http://stackoverflow.com/questions/7977071/how-to-dynamically-add-a-layout – topwik

回答

2

首先textView.setWidth(int)以像素为参数width

其次,您还应该在要添加的LinearLayout上设置布局参数。

你应该设置LayoutParams的方法如下:

ll.setLayoutParams(new LinearLayout.LayoutParams(
     LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT)); 

同为YOUT TextViews

+0

哦,雅,正直,谢谢你 – topwik

1

Ovidiu Latcu有一个很好的答案。一个很好的问题是:是否有一个原因,你不使用ListView(btw有什么情况下,他在做什么更好)?一个ListView有很多的机制,以帮助您防止内存不足

+0

好吧,我仍然相当绿色,当涉及到Android。我的理解是我不能在滚动视图中有ListView ....我希望我的表单布局可以滚动,然后历史类型列表可以是滚动视图内的列表视图,但不允许。或者你说整个相关的文本布局可能是一个列表视图...其中文本视图的内容可以是一个膨胀的行,然后在后执行,我可以膨胀一个不同的布局,我想要在那里呈现的项目,只是将它们添加到相同的ListView? – topwik

+0

开始想到它,我宁愿为这些动态内容项目中的每一个充气特定布局,而不是试图在代码中布置每个动态项目细节。仅仅设置已经按照我想要的方式布置的文本视图的文本会更容易 – topwik

相关问题