基于LinearLayout中创建自定义组件,宣布在XML布局 [英] Create custom component based on LinearLayout, declaring layout in XML

查看:125
本文介绍了基于LinearLayout中创建自定义组件,宣布在XML布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图建立在Android 1.5的复合控件(如​​这里描述),但没有带能够找到关于如何使用XML文件来指定一个布局要做到这一点任何很好的例子。我很好创建活动,然后使用下面的构造函数加载XML文件:

I've been trying to create a Compound Control in Android 1.5 (as described here) but havn't been able to find any good examples on how to do this using an XML file to specify a layout. I'm fine with creating an Activity and then loading an xml file using the following in the constructor:

setContentView(R.layout.main);

不过,我想做到这一点的LinearLayout的子类 - 这样我就可以用在其它的XML布局这种化合物成分。线沿线的东西:

However, I want to do this in subclass of LinearLayout - so I can use this compound component in other XML layouts. Something along the lines of:

public class CustomView extends LinearLayout
{
  public CustomView(Context context) {
       super(context);
       setupView();
  }
  public CustomView(Context context, AttributeSet attrs)
  {
      super(context, attrs);
      setupView();
  }
  public void setupView()
  {
    setContentView(R.layout.custom); // Not possible
  }
}

什么是打算这样做?

What is the correct way of going about doing this?

推荐答案

您有膨胀的布局为您的自定义视图:

You have to "inflate" the layout for your custom view:

LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutInflater.inflate(R.layout.custom, this, true);

这篇关于基于LinearLayout中创建自定义组件,宣布在XML布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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