如何从 XML 扩展扩展 LinearLayout
谁能建议一种改进这个 API8 示例的方法吗?虽然他们说视图可以在 XML 中定义,但实际上他们所做的是用 java 对其进行编码。我明白他们为什么想这么做。他们向扩展的 LinearLayout 添加了一些成员,并且这些值在运行时确定。
根据哦,宇宙中的每个人,布局指令应该转移到 XML。但对于这个应用程序来说,在运行时逻辑中继续按原样设置文本是有意义的。所以我们有一个混合方法。膨胀视图,然后填充动态文本。我很难弄清楚如何实现它。这是来源和我尝试过的。
private class SpeechView extends LinearLayout {
public SpeechView(Context context, String title, String words) {
super(context);
this.setOrientation(VERTICAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
...
我想,由于 LinearLayout 有一个 android:id="@+id/LinearLayout01",我应该能够在 OnCreate 中执行此操作,
SpeechView sv = (SpeechView) findViewById(R.id.LinearLayout01);
但它永远不会命中我添加的最小构造函数:
public class SpeechView extends LinearLayout {
public SpeechView(Context context) {
super(context);
System.out.println("Instantiated SpeechView(Context context)");
}
...
Can anyone suggest a way to improve this API8 example? While they say the views could have been defined in XML, what they have in fact done is code them in java. I see why they wanted to. They have added some members to an extended LinearLayout, and the values are determined at runtime.
According to oh, everyone in the universe, the layout directives should move to XML. But for this app, it makes sense to keep setting the text as-is in the runtime logic. So we've got a hybrid approach. Inflate the views, then populate the dynamic text. I'm having trouble figuring out how to accomplish it. Here's the source and what I tried.
from API8 Examples, List4.java
private class SpeechView extends LinearLayout {
public SpeechView(Context context, String title, String words) {
super(context);
this.setOrientation(VERTICAL);
// Here we build the child views in code. They could also have
// been specified in an XML file.
mTitle = new TextView(context);
mTitle.setText(title);
...
I figured since the LinearLayout has an android:id="@+id/LinearLayout01", I should be able to do this in the OnCreate
SpeechView sv = (SpeechView) findViewById(R.id.LinearLayout01);
but it never hits the minimal constructor I added:
public class SpeechView extends LinearLayout {
public SpeechView(Context context) {
super(context);
System.out.println("Instantiated SpeechView(Context context)");
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己刚刚遇到了这个确切问题。我认为你(我们)需要的是这个,但我仍在解决一些错误,所以我还不能肯定地说:
我会急于听到你是否有运气。
编辑:它现在对我来说就像这样。
I just ran into this exact problem myself. What I think you(we) need is this, but I'm still working through some bugs so I can't yet say for sure:
I'll be anxious to hear if you have any luck with it.
Edit: It's now working for me just like this.
看起来您夸大了位于文件 main_row.xml 中的布局。正确的?我的需求不同。我想膨胀 main.xml 中布局的 TextView 子级。
尽管如此,我还是使用了类似的解决方案。由于我已经在 onCreate 中从 XML 扩充了 LinearLayout,
剩下的就是在 View 构造函数中从 XML 扩充 TextView。我是这样做的。
R.id.roleHeading 是我正在膨胀的 TextView 的 id。
为了提高效率,我能够将 LayoutInflater 移至 Activity 成员,以便它仅实例化一次。
It looks like you inflated your Layout, which resides in the file main_row.xml. Correct? My need is different. I want to inflate a TextView child of the Layout I have in main.xml.
Still, I used a similar solution. Since I had already inflated the LinearLayout from XML in the onCreate
what remained is to inflate the TextView from XML in my View constructor. Here's how I did it.
R.id.roleHeading is the id of the TextView I'm inflating.
To increase efficiency I was able to move the LayoutInflater to an Activity member, so that it just gets instantiated once.