2

我有一个复杂的xml布局,它具有列表视图..列表视图中的行包含几个均匀间隔的文本字段。我使用textview来存储文本,然后最后添加所有的项目行...其工作完全正常。 但现在我有我不确定的情况下,我可能从web服务获得多少文本字段。因此我需要在运行时动态创建textview,填充它们,然后插入到列表中。 是否有在运行时声明,添加和填充新的textview字段? 还是有无法实现两个领域之间的间距?第一次调用第二个电话的在运行时构建布局android

__________________________ 
|_____|_____|_____|______| 

结果

结果

________________________________ 
|_____|_____|_____|______|______| 

我试图实现了低于(肯尼)提供的解决方案,但由于某种原因,我无法观点加入到下面是我的代码

public class HeaderAdapter extends ArrayAdapter<Header> { 
final Header[] listSymbols; 
private TextView textView; 
private LinearLayout row; 

public HeaderAdapter(Context context, int symResourceID, 
     Header[] objects) { 
    super(context, symResourceID, objects); 
    listSymbols = objects; 

} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) getContext() 
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    View row = inflater.inflate(R.layout.header_view, parent, false); 
    Header headerRec = listSymbols[position]; 
    for(int i = 0 ; i < listSymbols.length;i++){ 
     textView = new TextView(getContext()); 
     textView.setLayoutParams(new LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view 
       ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view 
     textView.setId(i); 
      row.add?? 

     } 
} 

调用这个主要活动

setContentView(R.layout.main); 

    headerList.add(new Header("Symbol","Quantity","Price","Price Yesterday","52 Week High","52 Week Low","Change","Days Gain","Days Gain %","Returns")); 

      Header[] tmpHeaderList = headerList.toArray(new Header[headerList.size()]); 

      ArrayAdapter<Header> headerAdapter = new HeaderAdapter(this,R.layout.twoway_header_view,tmpHeaderList); 
      headerListView.setAdapter(headerAdapter); 

XML布局file..the主文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <HorizontalScrollView android:layout_width="wrap_content" 
     android:layout_height="wrap_content" android:scrollbars="none" 
     android:id="@+id/headerHv"> 

     <ListView android:id="@+id/header_listView1" 
      android:layout_width="fill_parent" android:layout_height="wrap_content" 
      android:background="@color/white" android:cacheColorHint="#00000000" 
      android:smoothScrollbar="true" android:scrollbars="none" /> 
    </HorizontalScrollView> 
</LinearLayout> 

在该行的模板被定义

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    > 
    <TextView android:id="@+id/headerList" android:layout_width="80dp" 
     android:layout_height="wrap_content" android:textColor="#000000" 
     android:typeface="sans" android:textStyle="normal" /> 

</LinearLayout> 

回答

4

下面是我从动态列表生成自定义按钮的方式,你可以做同样的事情textViews:

//Setup Buttons 
    layout = (LinearLayout)findViewById(R.id.layoutBars); 
    int count = lBars.size(); 
    for(int i = 0; i< count;i++){ 
     final Bar b = lBars.get(i); 
     BarButton button = new BarButton(DDTBars.this, R.drawable.barsmall , b.getName().toUpperCase()); 
     button.setLayoutParams(new LayoutParams(
        ViewGroup.LayoutParams.FILL_PARENT, 
        ViewGroup.LayoutParams.WRAP_CONTENT)); 
     button.setId(i); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       //Run activity passing name of the bar to retrieve data 
       Intent i = new Intent(DDTBars.this, DDTBar.class); 
       i.putExtra("name", b.getName()); 
       startActivity(i); 
      } 
     }); 
     layout.addView(button);   
    } 

所以,你可以尝试:

//Setup TextViews 
    layout = (LinearLayout)findViewById(R.id.mylayout); 
    int count = myTextList.size(); 
    for(int i = 0; i< count;i++){ 
     TextView txtView = new TextView(this); 
     txtView.setLayoutParams(new LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, //Width of the view 
       ViewGroup.LayoutParams.WRAP_CONTENT));//Height of the view 
     txtView.setId(i); 
     layout.addView(txtView);    
    } 
+0

@Kenny ...谢谢...但在这里你使用BarButton,有没有像这样的textview? –

+0

BarButton是我制作的自定义按钮,所有按钮都扩展了View类,textViews也扩展了视图类。所以你可以做同样的事情,但添加一个textView而不是一个按钮。 – Kenny

+1

我已经扩展了我的答案以向您展示,请将问题标记为已回答,将其从未答复的列表中删除。 – Kenny

0

你能做到这一点的代码的文件。在一个循环中声明TextView,并使用RelativeLayout将它们放置在一起。