我已经创建了一个TableRow布局模型,作为扩展TableRow的自定义视图的模板。
我的愿望是膨胀扩展类中的行,并将已膨胀的实例作为扩展行。
在伪码中:
public class MyTableRow extends TableRow{
if(conditionA)
this = (TableRow)inflate(R.layout.myModelRowA);
if(conditionB)
this = (TableRow)inflate(R.layout.myModelRowB);
etc...
}
然后当我把这些行加到表中时,我会像往常一样.
myTable.addView(new MyTableRow(this));
因此,问题是,显然我不能将事情分配给这个,即使从逻辑上讲,这是非常有意义的,因为在上面的示例中,这个是一个TableRow。
还有别的办法吗?我知道这看起来很愚蠢,但我目前的选择是将膨胀的TableRow作为MyTableRow的一个成员,它可以工作,但是很难看(而且感觉不太好)。
我漏掉了什么明显的东西吗?还是我应该满足于我所拥有的?
发布于 2011-10-11 22:55:33
使用标记 (参见这里)来定义您的行A和行B布局,而不是:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Other component declarations here -->
</merge>
然后在扩展行构造函数中膨胀其中一个布局:
public MyTableRow(Context context, AttributeSet attrs) {
super(context, attrs);
if (condA) {
inflate(context, R.layout.RowA, this);
} else {
inflate(context, R.layout.RowB, this);
}
}
然后,在表布局中使用扩展行而不是TableRow:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<your.package.name.MyTableRow>
</your.package.name.MyTableRow>
</TableLayout>
发布于 2011-10-11 22:38:58
不确定您到底在寻找什么,但您可以获得视图并执行get上下文。类似于视图=(TableRow)膨胀(R.layout.myModelRowA);上下文ctx = view.getContext();
https://stackoverflow.com/questions/7735855
复制