2012-09-27 76 views
-1

我有此XML布局文件列表:安卓:我试图视图添加到

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/gray" 
    android:orientation="vertical" > 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_margin="10dp" 
    android:background="@color/black" 
    android:layout_height="wrap_content" > 

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

     <TextView 
      android:id="@+id/s_hour" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/gray" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 

     <TextView 
      android:id="@+id/e_hour" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textColor="@color/gray" 
      android:text="Large Text" 
      android:textAppearance="?android:attr/textAppearanceLarge" /> 
    </LinearLayout> 

</RelativeLayout> 

</LinearLayout> 

,我有这行代码:

class WorkingView extends LinearLayout 
{ 

public WorkingView(Context context,String str) { 
    super(context); 
    // TODO Auto-generated constructor stub 
    setContentView(R.layout.work_data); 

    Text s_hour = (Text) findViewById(R.id.s_hour); 
    s_hour.setData(str); 
    } 

} 

,并从另一本线延伸活动主类:

private void readData() { 
    WorkingView wv = new WorkingView(this,"hello"); 
    list.addView(wv); 
} 

这一切工作正常,直到代码调用这个READDATA()方法。

我不明白什么是错的?

+0

请提供有关的问题是什么的详细信息。你的代码崩溃了,它不是在做你期望的吗?如何定义变量列表?它是一个ListView?如果你想填充列表,你不能只添加视图,最好的方法是使用一个适配器,看看这里:https://developer.android.com/guide/topics/ui/declaring-layout .html#AdapterViews – Alf

+0

是的应用程序崩溃 –

+0

请告诉我们你运行这段代码时得到的异常。 – Hemant

回答

1

对于你在做什么你不应该使用一个列表。

如果您只是想在另一个视图下添加一个视图: 请将linearLayout作为“容器”,然后将其添加到该视图。

如果您有一堆数据需要在一个或多个属性下互相排列,那么将使用列表。为此,您需要查看适配器,arrayadapter更简单,而cursoradapter更高级。

此外,你的方法可能会崩溃,因为你不膨胀你的xml。对于通胀的观点看here

对于滚动功能考虑ScrollViewhere is a guide to use it

+0

我确实想在另一个下添加一个视图,但是,如果开始获取太多,我也想向下滚动,以及你的意思是膨胀我的XML? –

+0

更新了我的答案 –