膨胀成"本"? [英] Inflate into "this"?

查看:186
本文介绍了膨胀成"本"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建没有太多的XML应用程序,创建视图编程。我想切换到XML。所以我写了一个RelativeLayout的一个XML文件,我需要把它夸大成具有所有的实现逻辑现有类(RelativeLayout的子类,当然)。

如何进入膨胀本在构造函数?

顺便说一句,什么是真正的XML的优势在哪里?当我创建了code的看法,我缩放字体和图像,并左右移动的看法取决于屏幕的大小,方向,长宽比等。随着XML的方式,我不得不创建一个单独的XML所有可能的配置...

构造code:

 公共OrderEditControl()
  {
    超级(LmcActivity.W.getApplicationContext());
    资源解析度= LmcActivity.W.getResources();
    setBackgroundColor(Color.TRANSPARENT);
    标题= res.getStringArray(R.array.item_list_columns);
    宽度=新INT [headers.length]    createLabels();
    createButtons();    的LayoutParams LP =新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    lp.addRule(ALIGN_PARENT_TOP);
    lp.addRule(RIGHT_OF,标签[LabelType.CUSTOMER.ordinal()]的getId());
    lp.addRule(LEFT_OF,按钮[ButtonType.FIND_CUSTOMER.ordinal()]的getId());    customerView =新的TextView(LmcActivity.W.getApplicationContext());
    customerView.setTextColor(Color.BLACK);
    customerView.setId(400);
    customerView.setTypeface(Typeface.DEFAULT_BOLD);
    customerView.setGravity(Gravity.CENTER_VERTICAL);
    addView(customerView,LP);    LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
    lp.addRule(ALIGN_TOP,标签[LabelType.SHIP_TYPE.ordinal()]的getId());
    lp.addRule(ALIGN_BOTTOM,标签[LabelType.SHIP_TYPE.ordinal()]的getId());
    lp.addRule(RIGHT_OF,标签[LabelType.SHIP_TYPE.ordinal()]的getId());    shipSpinner =新的微调(LmcActivity.W);
    shipSpinner.setId(401);
    shipSpinner.setAdapter(shipAdapter);
    shipSpinner.setOnItemSelectedListener(本);
    addView(shipSpinner,LP);    deliveryView =新的EditText(LmcActivity.W.getApplicationContext());
    deliveryView.setGravity(Gravity.CENTER_VERTICAL);
    deliveryView.setSingleLine();
    deliveryView.setId(402);
    addView(deliveryView);    LP =新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
    lp.addRule(RIGHT_OF,标签[LabelType.COMMENTS.ordinal()]的getId());
    lp.addRule(LEFT_OF,按钮[ButtonType.ITEMS.ordinal()]的getId());
    lp.addRule(ALIGN_TOP,标签[LabelType.COMMENTS.ordinal()]的getId());    评论查看=新的EditText(LmcActivity.W.getApplicationContext());
    commentView.setGravity(Gravity.TOP);
    commentView.setId(403);
    addView(评论查看,LP);    LP =新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
    lp.addRule(以下commentView.getId());
    ITEMLIST =新的ListView(LmcActivity.W.getApplicationContext());
    itemList.addHeaderView(createRow(NULL,NULL),空,假);
    itemList.setOnItemClickListener(本);
    itemList.setAdapter(itemAdapter);
    itemList.setCacheColorHint(0);
    itemList.setBackgroundColor(Color.TRANSPARENT);
    itemList.setId(404);
    addView(ITEMLIST,LP);    奠定了[0] =新LayParm(假);
    规定[1] =新LayParm(真);
  }  / **创建视图的按钮* /
  私人无效createButtons()
  {
    的for(int i = 0; I< N_BUT ++ I)
    {
      按钮,但= I == ButtonType.ITEMS.ordinal()?
          新TextGlassButton(2.4f,LmcActivity.W.getResources()的getString(R.string.items),Color.WHITE。):
          新EffGlassButton(1.2F,butEffects [I]);
      but.setId(BUT_ID + I);
      but.setOnClickListener(本);
      按钮[i] =但是,      如果(ⅰ== ButtonType.DATE.ordinal())
        addView(但);
      其他
      {
        的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
        如果(ⅰ2)
          lp.addRule(ALIGN_PARENT_TOP);
        其他
          lp.addRule(以下BUT_ID + I - 2);        如果(我%2 == 0)
          lp.addRule(ALIGN_PARENT_RIGHT);
        其他
          lp.addRule(LEFT_OF,BUT_ID + I - 1);        addView(但是,LP);
      }
    }
  }  / **创建文本标签* /
  私人无效createLabels()
  {
    涂料粉刷= AFDraw.W.textPaint;
    paint.setTextSize(Universe.TEXT_SIZE);
    paint.setTypeface(LmcActivity.W.defaultTypeface);    的String []标题= LmcActivity.W.getResources()getStringArray(R.array.order_labels)。    的for(int i = 0; I< titles.length ++ I)
    {
      的LayoutParams LP =新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
      lp.addRule(ALIGN_PARENT_LEFT);      如果(我== 0)
        lp.addRule(ALIGN_PARENT_TOP);
      其他
        lp.addRule(以下LABEL_ID + I - 1);      TextView的电视=新的TextView(LmcActivity.W.getApplicationContext());
      tv.setText(标题[I]);
      tv.setTextColor(Color.BLACK);
      tv.setId(LABEL_ID + I);
      tv.setTypeface(LmcActivity.W.defaultTypeface);
      tv.setGravity(Gravity.CENTER_VERTICAL);
      标签由[i] =电视;
      addView(电视,LP);      labelWidth = Math.max(labelWidth,paint.measureText(标题[I]));
    }    labelWidth + = Universe.TEXT_SIZE * 0.5F;
    dateWidth = paint.measureText(00/00/00)+ Universe.TEXT_SIZE * 1.5F;
  }


解决方案

@scriptocalypse一般是对的,但一些子类布局和膨胀的自定义布局这一类可以帮助区分不同的抽象。有这么多坏的教程中,一切都在活动完成。我看到,世界正在添加新的程序员将code只有废话寻找应用程序。

使用自定义布局,您可以在活动只能做这样的事情:

  medicineView.putMedicine(medicineList);

而不是所有蹩脚的转接器创作和寻找意见...

首先您应该创建自定义视图的一些观点:

 < RelativeLayout的...>
    < - !你把你的一切都在这里的观点 - >
< / RelativeLayout的>

其次如果你与你的观点sattisfied,你应该根改变的合并的标记:

 <合并...>
    < - !你把你的一切都在这里的观点 - >
< /合并>

这是非常重要的。我们首先RelativeLayout的标签设计,以IDE知道如何绘制的布局,以及如何做落成。但是,如果我们离开它,因为它是,我们将在两个嵌套RelativeLayouts最终这将是类似的东西到底:

 < RelativeLayout的...> <! - 那是你的类 - >
    < RelativeLayout的...> <! - 这是从布局膨胀 - >
        < - !你把你的一切都在这里的观点 - >
    < / RelativeLayout的>
< / RelativeLayout的>

如果你改变你的布局,以合并,那么它​​会是这样的:

 < RelativeLayout的...> <! - 那是你的类 - >
    <合并...> <! - 这是从布局膨胀 - >
        < - !你把你的一切都在这里的观点 - >
    < /合并>
< / RelativeLayout的>

和将被合并到其根

 < RelativeLayout的...> <! - 那是你的课,合并布局 - >
    < - !你把你的一切都在这里的观点 - >
< / RelativeLayout的>

在结束您必须继承要求查看或ViewGroup中:

 公共类CustomView扩展RelativeLayout的{
    公共CustomView(上下文的背景下){
        超级(上下文);
        初始化();
    }    公共CustomView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        初始化();
    }    公共CustomView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        初始化();
    }    私人无效初始化(){
        LayoutInflater充气= LayoutInflater.from(的getContext());
        inflater.inflate(R.id.your_layout,这一点,真正的);        //找到你的意见,集中处理等。
    }
}

用法

就像@scriptocalypse已经说了。在另一种布局中,可以使用这样的:

 < SomeLayout>
    < com.foo.CustomView>
< / SomeLayout>

I've been creating apps without much XML, creating views programmatically. I'd like to switch to XML. So I wrote an XML file for a RelativeLayout, and I need to inflate it into an existing class (a subclass of RelativeLayout, of course) that has all the implementation logic.

How do I inflate into "this" in the constructor?

By the way, what's really the advantage of XML? When I create views in the code, I scale fonts and images and also move views around depending on the screen's size, orientation, aspect ratio, etc. With XML approach, I'd have to create a separate XML for all possible configurations...

Constructor code:

  public OrderEditControl()
  {
    super(LmcActivity.W.getApplicationContext());
    Resources res = LmcActivity.W.getResources();
    setBackgroundColor(Color.TRANSPARENT);
    headers = res.getStringArray(R.array.item_list_columns);
    widths = new int[headers.length];

    createLabels();
    createButtons();

    LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(ALIGN_PARENT_TOP);
    lp.addRule(RIGHT_OF, labels[LabelType.CUSTOMER.ordinal()].getId());
    lp.addRule(LEFT_OF, buttons[ButtonType.FIND_CUSTOMER.ordinal()].getId());

    customerView = new TextView(LmcActivity.W.getApplicationContext());
    customerView.setTextColor(Color.BLACK);
    customerView.setId(400);
    customerView.setTypeface(Typeface.DEFAULT_BOLD);
    customerView.setGravity(Gravity.CENTER_VERTICAL);
    addView(customerView, lp);

    lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(ALIGN_TOP, labels[LabelType.SHIP_TYPE.ordinal()].getId());
    lp.addRule(ALIGN_BOTTOM, labels[LabelType.SHIP_TYPE.ordinal()].getId());
    lp.addRule(RIGHT_OF, labels[LabelType.SHIP_TYPE.ordinal()].getId());

    shipSpinner = new Spinner(LmcActivity.W);
    shipSpinner.setId(401);
    shipSpinner.setAdapter(shipAdapter);
    shipSpinner.setOnItemSelectedListener(this);
    addView(shipSpinner, lp);

    deliveryView = new EditText(LmcActivity.W.getApplicationContext());
    deliveryView.setGravity(Gravity.CENTER_VERTICAL);
    deliveryView.setSingleLine();
    deliveryView.setId(402);
    addView(deliveryView);

    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
    lp.addRule(RIGHT_OF, labels[LabelType.COMMENTS.ordinal()].getId());
    lp.addRule(LEFT_OF, buttons[ButtonType.ITEMS.ordinal()].getId());
    lp.addRule(ALIGN_TOP, labels[LabelType.COMMENTS.ordinal()].getId());

    commentView = new EditText(LmcActivity.W.getApplicationContext());
    commentView.setGravity(Gravity.TOP);
    commentView.setId(403);
    addView(commentView, lp);

    lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    lp.addRule(BELOW, commentView.getId());
    itemList = new ListView(LmcActivity.W.getApplicationContext());
    itemList.addHeaderView(createRow(null, null), null, false);
    itemList.setOnItemClickListener(this);
    itemList.setAdapter(itemAdapter);
    itemList.setCacheColorHint(0);
    itemList.setBackgroundColor(Color.TRANSPARENT);
    itemList.setId(404);
    addView(itemList, lp);

    lays[0] = new LayParm(false);
    lays[1] = new LayParm(true);
  }

  /** create the view's buttons */
  private void createButtons()
  {
    for (int i = 0; i < N_BUT; ++i)
    {
      Button but = i == ButtonType.ITEMS.ordinal() ?
          new TextGlassButton(2.4f, LmcActivity.W.getResources().getString(R.string.items), Color.WHITE) :
          new EffGlassButton(1.2f, butEffects[i]);
      but.setId(BUT_ID + i);
      but.setOnClickListener(this);
      buttons[i] = but;

      if (i == ButtonType.DATE.ordinal())
        addView(but);
      else
      {
        LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        if (i < 2)
          lp.addRule(ALIGN_PARENT_TOP);
        else
          lp.addRule(BELOW, BUT_ID + i - 2);

        if (i % 2 == 0)
          lp.addRule(ALIGN_PARENT_RIGHT);
        else
          lp.addRule(LEFT_OF, BUT_ID + i - 1);

        addView(but, lp);
      }
    }
  }

  /** create text labels */
  private void createLabels()
  {
    Paint paint = AFDraw.W.textPaint;
    paint.setTextSize(Universe.TEXT_SIZE);
    paint.setTypeface(LmcActivity.W.defaultTypeface);

    String[] titles = LmcActivity.W.getResources().getStringArray(R.array.order_labels);

    for (int i = 0; i < titles.length; ++i)
    {
      LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
      lp.addRule(ALIGN_PARENT_LEFT);

      if (i == 0)
        lp.addRule(ALIGN_PARENT_TOP);
      else
        lp.addRule(BELOW, LABEL_ID + i - 1);

      TextView tv = new TextView(LmcActivity.W.getApplicationContext());
      tv.setText(titles[i]);
      tv.setTextColor(Color.BLACK);
      tv.setId(LABEL_ID + i);
      tv.setTypeface(LmcActivity.W.defaultTypeface);
      tv.setGravity(Gravity.CENTER_VERTICAL);
      labels[i] = tv;
      addView(tv, lp);

      labelWidth = Math.max(labelWidth, paint.measureText(titles[i]));
    }

    labelWidth += Universe.TEXT_SIZE * 0.5f;
    dateWidth = paint.measureText("00/00/00") + Universe.TEXT_SIZE * 1.5f;
  }

解决方案

@scriptocalypse is generally right, but subclassing some layouts and inflating custom layout to this class helps to separate different abstractions. There are so many bad tutorials, in which everything is done in the Activity. I see that the world's new comming programmers will code only crap looking applications.

With custom layout you can do in Activity only such a thing:

medicineView.putMedicine(medicineList);

instead of all crappy adapter creations and looking for views...

Firstly you should create some view for your custom View:

<RelativeLayout ...>
    <!-- You put all your views here -->
</RelativeLayout>

Secondly if you are sattisfied with your view, you should change the root to merge tag:

<merge ...>
    <!-- You put all your views here -->
</merge>

This is very important. We begin design with RelativeLayout tags in order to IDE know how to draw layouts, and how to do completions. But if we leave it as it is, we will end up in two nested RelativeLayouts it will be something like that in the end:

<RelativeLayout ...>    <!-- That is your class -->
    <RelativeLayout ...> <!-- This is inflated from layout -->  
        <!-- You put all your views here -->
    </RelativeLayout>
</RelativeLayout>

If you change your layout to "merge" then it will look like this:

<RelativeLayout ...>    <!-- That is your class -->
    <merge...> <!-- This is inflated from layout -->  
        <!-- You put all your views here -->
    </merge>
</RelativeLayout>

and will be merged to its root:

<RelativeLayout ...>    <!-- That is your class, merged with layout -->
    <!-- You put all your views here -->
</RelativeLayout>

At the end you must subclass demanded View or ViewGroup:

public class CustomView extends RelativeLayout {
    public CustomView(Context context) {
        super(context);
        initialize();
    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initialize();
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initialize();
    }

    private void initialize() {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.id.your_layout, this, true);

        // find your views, set handlers etc.
    }
}

Usage

Just like @scriptocalypse already said. In another layout you use this like that:

<SomeLayout>
    <com.foo.CustomView>
</SomeLayout>

这篇关于膨胀成&QUOT;本&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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