奇怪的(可能是愚蠢的)尝试将视图膨胀为扩展 View 对象的实例
我创建了一个 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));
所以,问题是,显然我无法将事物分配给this,即使在逻辑上也是如此这是完全有道理的,因为在上面的示例中 this 是一个 TableRow。
还有其他方法可以做到这一点吗?我知道这看起来很愚蠢,但我当前的替代方案是将膨胀的 TableRow 作为 MyTableRow 的成员,这可以工作,但真的很难看(并且感觉不是很 OO)。
我错过了一些明显的事情吗?或者我应该满足于我所拥有的吗?
I have created a TableRow layout model which I'm using as a template for a custom View that extends TableRow.
My desire is to inflate the row inside the extended class, AND have that inflated instance BE the extended row.
In pseudo code:
public class MyTableRow extends TableRow{
if(conditionA)
this = (TableRow)inflate(R.layout.myModelRowA);
if(conditionB)
this = (TableRow)inflate(R.layout.myModelRowB);
etc...
}
And then when I'm adding these rows to the table I'd proceed as usual...
myTable.addView(new MyTableRow(this));
So, the issue is that obviously I can't assign things to this even if logically it makes perfect sense, since in the example above this is a TableRow.
Is there another way to do this? I know it looks stupid, but my current alternative is to house the inflated TableRow as a member of MyTableRow, which works, but is really ugly (and doesn't feel very OO).
Am I missing something obvious? Or should I just settle for what I have?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用标签 定义 A 行和 B 行布局(请参阅 此处)而不是:
然后在扩展行构造函数中膨胀这些布局之一:
然后在表布局中使用扩展行而不是 TableRow:
Define your Row A and Row B layouts with tag <merge> (see here) instead of <TableRow>:
then inflate one of these layouts in your extended row constructor:
Then in your Table layout use your extended Row instead of TableRow:
不确定您到底在寻找什么,但您可以获取视图并获取上下文。
像这样的东西
视图视图 = (TableRow)inflate(R.layout.myModelRowA);
上下文 ctx = view.getContext();
Not sure what you are exactly looking for but you can get the view and do a get context.
Something like
View view = (TableRow)inflate(R.layout.myModelRowA);
Context ctx = view.getContext();