在运行时创建的复合Android组件
|
问候我正在从事一个项目,并认为我会汇总一个复合组件。我将它们布置在xml中,并编写了一个扩展LinearLayout的新类。基本上,我将在一个Activity中使用其中的五六个。我想以编程方式在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);
...
}
在“活动”内部,这两个解决方案似乎是我所寻找的最接近的解决方案。
不知道从何处获取使用此方法的attrs:
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:二进制XML文件第8行:膨胀类错误。我觉得xml看起来还不错。我可能会感到困惑的一件事是在哪里呼叫充气机。现在,我正在自定义类的onFinishInflate()和“活动”中调用充气机,如下所示:
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>
没有找到相关结果
已邀请:
1 个回复
讹巳漓把备
成为
的完整类名(包括包名)。然后ѭ8会知道该怎么办。 编辑:同样,
选项不是真的可行(或者至少我不这么认为)。我还没有找到一种在运行时生成实际的“ 9”对象实例的方法:没有公共构造函数,我希望这是框架在无法访问的框架下所做的另一件事。