0

我想在扩展视图的类中调用我的 XML 布局文件。我无法在其中执行 oncreate 功能,因为它不扩展 Activity。我的事件在一个类文件中,我在另一个类文件中调用该类;但我想在 XML 布局文件上执行这些事件。

这是我的代码:

演示.java

public class Demo extends Activity
{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new another(this));
}

另一个.java

public class another extends View
{
      public anotherView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        init();
    }

    public anotherView(Context context) {
        super(context);
        init();
    }

    public anotherView(Context context, AttributeSet attrs) {
        super(context, attrs);  

        init();
    }

    private void init() {
        setWillNotDraw(false);
       ...
        ..
        ..
}

和 XML 文件

abc.xml
..
..

怎么做?

4

2 回答 2

0

如果你想要的是从你的 XML 中获取一个项目,你应该调用
[yourviewType]View v1 = (Cast-to-type-of-view)context.findViewById(R.id.idOfView);

例子 : TextView tv1 = (TextView)findViewById(R.id.aboutText);

这是你需要的吗?

于 2012-03-22T11:39:29.623 回答
0

如果您想将一个项目添加到您的视图中,您可以随时对其进行充气。

LayoutInflater inflater = (LayoutInflater)    
getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View view = inflater.inflate(R.layout.ABC, null);
addView(view);
于 2012-03-27T23:01:29.167 回答