0

问候。我正在做一个项目,并认为我会卷起一个复合组件。我将它们放在 xml 中,并编写了一个扩展 LinearLayout 的新类。基本上,我在一个活动中需要大约五六个。我想以编程方式在 Activity 中创建这些,因为我不知道在运行之前我需要多少。另外,我创建了一个 xml 文件来设置它们,我没有包括在下面。我发现的每种方法都有问题。

以下是我创建的扩展 LinearLayout 的类中的方法段:

public ExtendedLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
}

@Override
protected void onFinishInflate() {
    super.onFinishInflate();
    ((Activity)getContext()).getLayoutInflater().inflate(R.layout.extended_linear_layout, this);
    ...
}

在 Activity 内部,这两个解决方案似乎最接近我正在寻找的解决方案。

不知道从哪里获取使用此方法的属性:

ExtendedLinearLayout customViewClass = new ExtendedLinearLayout(this, attrs);
layout.addView(customViewClass)

不知道如何通过此方法指定充气机应使用 ExtendedLinearLayout:

LayoutInflater inflater = LayoutInflater.from(this);
inflater.inflate(R.layout.extended_linear_layout, parent_layout);

我可能会满足于使用 ListView - 但对于五到六个项目来说似乎有点过分了。此外,之前曾与 Flex 合作过,这至少看起来是一个合理的问题解决方案。

谢谢,兰德尔

- - - - - - - - 更新 - - - - - - - - - -

我已经尝试过下面讨论的方法。设置完所有内容后,我将收到 InflationException: Binary XML file line #8: Error inflating class。我觉得xml 看起来还不错。我可能会感到困惑的一件事是在哪里调用充气机。现在,我在自定义类的 onFinishInflate() 以及 Activity 中调用充气器,如下所示:

public class ExtendedLinearLayout extends LinearLayout {
    ...
public ExtendedLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.extended_linear_layout, null);
        ...
    }
}

在活动中:

public class TestActivity extends Activity {
    LinearLayout list;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        list = (LinearLayout) findViewById(R.id.simple_list);
        runTestCode();
    }
    private void runTestCode() {
        LayoutInflater inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ExtendedLinearLayout layoutOne = 
                (ExtendedLinearLayout) inflater.inflate(R.layout.extended_linear_layout, list);
    }
}

谢谢,希望你有时间看看。我喜欢通过在网上四处挖掘来找出东西,但从昨天早上开始,这个已经让我煮了好几个小时了。我距离将其重构为 ListAdapter 大约有 2 英寸的距离。

- - - - - - - - 更新 - - - - - - - - - -

这是xml文件

<?xml version="1.0" encoding="utf-8"?>
<com.anotherandroidblog.ExtendedLinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    >
    <TextView
        android:id="@+id/text_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text=""
        ></TextView>
    <TextView
        android:id="@+id/text_three"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        ></TextView>
</com.anotherandroidblog.ExtendedLinearLayout>
4

1 回答 1

0

您需要在 XML 文件中使用完整的类名:不要将根元素设为LinearLayout完整的类名ExtendedLinearLayout(包括包名)。然后LayoutInflater就会知道该怎么做。

编辑:另外,这个attrs选项并不是真的可行(或者至少我不这么认为)。我还没有找到一种在运行时生成实际attrs对象实例的方法:没有公共构造函数,我希望这是框架在引擎盖下所做的另一件事,只是无法访问。

于 2011-05-27T14:10:15.987 回答