如何从充气扩展XML的LinearLayout [英] How to Inflate Extended LinearLayout from XML

查看:136
本文介绍了如何从充气扩展XML的LinearLayout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以提出一个方法来改善这种API8例子吗?虽然他们说的意见,可能在XML中被定义,他们实际上做的事情是code他们在java中。我明白为什么他们要。他们已经增加了一些成员扩展的LinearLayout,并在运行时确定的值。

据哦,大家都在宇宙中,布局指令应该转移到XML格式。但对于这个应用程序,这是有道理的,以保持设定的文本,在运行逻辑。所以,我们已经有了一个混合的方法。充气的意见,然后填充动态文本。我无法搞清楚如何实现它。这里的来源,我试过了。

从<一个href=\"http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/List4.html\">API8例如,List4.java

 私有类SpeechView扩展的LinearLayout {
     公共SpeechView(上下文的背景下,标题字符串,字符串字){
        超级(上下文);        this.setOrientation(垂直);        //这里我们建立code中的子视图。它们也可以具有
        //在XML文件中被指定。        mTitle =新的TextView(背景);
        mTitle.setText(职称);
        ...

我想既然有LinearLayout中的机器人:ID =@ + ID / LinearLayout01,我应该能够做到这一点在OnCreate

  SpeechView SV =(SpeechView)findViewById(R.id.LinearLayout01);

但它从来没有碰到最小的构造我说:

 公共类SpeechView扩展的LinearLayout {
       公共SpeechView(上下文的背景下){
          超级(上下文);
          的System.out.println(实例化SpeechView(上下文的背景下));
       }
       ...


解决方案

看起来你夸大你的布局,它驻留在文件main_row.xml。正确?我的需要是不同的。我想夸大布局的TextView的孩子,我有main.xml中。

不过,我用了一个类似的解决方案。既然我已经从XML在OnCreate已经膨胀了的LinearLayout

 的setContentView(R.layout.main);

还有哪些是吹的XML TextView的在我看来构造函数。以下是我做的。

  LayoutInflater李= LayoutInflater.from(背景);
LL的LinearLayout =(的LinearLayout)li.inflate(R.layout.main,这一点);
TextView的mTitle =(TextView中)ll.findViewById(R.id.roleHeading);

R.id.roleHeading是我充气的TextView的id。

 &LT; TextView的机器人:ID =@ + ID / roleHeading... /&GT;

为了提高效率,我能够在LayoutInflater移动到活动成员,因此,它只是被实例化一次。

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)");
       }
       ...

解决方案

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.

这篇关于如何从充气扩展XML的LinearLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆