-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() 方法。

我不明白出了什么问题?

4

1 回答 1

1

对于您正在做的事情,您不应该使用列表。

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

如果您有一堆数据,这些数据都需要用一个或多个属性排列在彼此的下方,则使用列表。为此,您需要研究适配器,arrayadapter 更简单,而 cursoradapter 更高级。

此外,您的方法可能会崩溃,因为您没有膨胀您的 xml。对于视图膨胀看这里

对于滚动功能,请查看ScrollView这是使用它的指南

于 2012-09-27T08:41:16.793 回答