如何从 XML 扩展扩展 LinearLayout

发布于 2024-10-10 15:37:36 字数 1271 浏览 4 评论 0原文

谁能建议一种改进这个 API8 示例的方法吗?虽然他们说视图可以在 XML 中定义,但实际上他们所做的是用 java 对其进行编码。我明白他们为什么想这么做。他们向扩展的 LinearLayout 添加了一些成员,并且这些值在运行时确定。

根据哦,宇宙中的每个人,布局指令应该转移到 XML。但对于这个应用程序来说,在运行时逻辑中继续按原样设置文本是有意义的。所以我们有一个混合方法。膨胀视图,然后填充动态文本。我很难弄清楚如何实现它。这是来源和我尝试过的。

来自 API8 示例,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);
        ...

我想,由于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

高跟鞋的旋律 2024-10-17 15:37:36

我自己刚刚遇到了这个确切问题。我认为你(我们)需要的是这个,但我仍在解决一些错误,所以我还不能肯定地说:

public class SpeechView extends LinearLayout {
        public SpeechView(Context context) {
           super(context);
           View.inflate(context, R.layout.main_row, this);
        }
        ...

我会急于听到你是否有运气。

编辑:它现在对我来说就像这样。

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:

public class SpeechView extends LinearLayout {
        public SpeechView(Context context) {
           super(context);
           View.inflate(context, R.layout.main_row, this);
        }
        ...

I'll be anxious to hear if you have any luck with it.

Edit: It's now working for me just like this.

哭泣的笑容 2024-10-17 15:37:36

看起来您夸大了位于文件 main_row.xml 中的布局。正确的?我的需求不同。我想膨胀 main.xml 中布局的 TextView 子级。

尽管如此,我还是使用了类似的解决方案。由于我已经在 onCreate 中从 XML 扩充了 LinearLayout,

setContentView(R.layout.main);

剩下的就是在 View 构造函数中从 XML 扩充 TextView。我是这样做的。

LayoutInflater li = LayoutInflater.from(context);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.main, this);
TextView mTitle = (TextView) ll.findViewById(R.id.roleHeading);

R.id.roleHeading 是我正在膨胀的 TextView 的 id。

<TextView android:id="@+id/roleHeading" ... />

为了提高效率,我能够将 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

setContentView(R.layout.main);

what remained is to inflate the TextView from XML in my View constructor. Here's how I did it.

LayoutInflater li = LayoutInflater.from(context);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.main, this);
TextView mTitle = (TextView) ll.findViewById(R.id.roleHeading);

R.id.roleHeading is the id of the TextView I'm inflating.

<TextView android:id="@+id/roleHeading" ... />

To increase efficiency I was able to move the LayoutInflater to an Activity member, so that it just gets instantiated once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文