2012-03-28 104 views
4

我有一个应用程序,点击后,显示新闻内容的活动。我想在底部显示注释,这些注释是在异步任务中动态加载的。以编程方式添加视图(线性)布局(即滚动视图内)

一种方法是使用ListView和自定义的ArrayAdapter,但是,我不得不把ListView放在一个ScrollView中,这是一个问题,即使我手动重写ListView的高度。它显示一个列表项目,以及下一个列表项目的一部分。

另一种方法是将LinearLayout定义为注释的持有者,而不是膨胀在自己的xml文件中定义的另一个LinearLayouts,填充它的内容,附加到持有者的视图。从程序的角度来看,它工作正常,只是它在新闻内容的下方推荐评论。就好像,它会在内容od news(内容重叠)下面创建另一个可滚动的评论视图。

是否有任何其他方式来做到这一点,与列表无关,或者我如何使其他方法工作。

从保持新闻内容的XML布局相关的代码片段是:

<LinearLayout> //global layout 
    <LinearLayout> 
     //views that hold title, date, content 
    </LinearLayout>  

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/commentsHolder" 
      android:orientation="vertical"> 

     <View 
      android:layout_width="fill_parent" 
      android:layout_height="2dp" 
      android:background="#ed1b24"/> 
     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="fill_parent" 
      android:text="KOMENTARI" 
      android:layout_marginLeft="5dp"/> 
     <View 
      android:layout_width="fill_parent" 
      android:layout_height="1dp" 
      android:background="#ed1b24"/> 

     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="0dip" 
      android:orientation="vertical" 
      android:id="@+id/comments_holder_view" 
      android:layout_weight="1"> 
     </LinearLayout> 


    </LinearLayout> 

</LinearLayout> 

,并对应于一个注释代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/comments_holder_item" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="5dp"> 

<TextView 
    android:id="@+id/comment_content" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@drawable/comments_back_details" 
    android:text="Ovde ide tekst komentara, koji se, naravno, dinamicki dodaje."/> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

    <TextView 
     android:id="@+id/comment_author" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/categoryStyle" 
     /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text=" | " 
     style="@style/categoryStyle" 
     /> 

    <TextView 
     android:id="@+id/comment_pubdate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="@style/categoryStyle"/> 

</LinearLayout> 

<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="1dp" 
    android:orientation="horizontal" 
    android:layout_margin="5dp" 
    android:background="#d1d1d1"> 
</LinearLayout> 

非常感谢您的任何回复。这对我来说非常重要。

回答

2

我不是很确定你如何膨胀你的comments,但这里是我如何做到这一点。

声明的评论Layout作为LinearLayout,并给它一个id,然后得到那个LinearLayout参考:

LinearLayout commentsLayout=(LinearLayout)findViewById(R.id.commentsLayoutId); 

,然后只需添加子Views到此布局:

commentsLayout.addView(newComment); 
+0

通常你会使用'getLayoutInflater()。inflate(R.layout.comment,null);'从你的活动 – njzk2 2012-03-28 14:05:13

+0

我完全像你发布的Ovidiu,它工作正常,但不知何故,线性布局(ID为comments_holder_vie w)持有的评论是在其他布局下。如果我静态地(手动)包括几个评论布局,一切都会显示正常。看起来问题出现在动态添加这些视图时。 Thanx回复,但。 – Dimmy3 2012-03-28 14:09:13

+0

请发表一些代码,说明如何创建添加的视图(注释)? – 2012-03-28 14:16:51

相关问题