2012-03-22 50 views
0

我想在扩展视图的类中调用我的XML布局文件。我不能执行oncreate函数,因为它没有扩展Activity。我的事件在一个类文件中,我在另一个类文件中调用该类;但我想在XML布局文件上执行这些事件。Android - 在扩展类中调用xml布局文件查看

这里是我的代码:

Demo.java

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

another.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 
.. 
.. 

怎么办呢?

+1

你能发布abc.xml吗? – Blackbelt 2012-03-22 11:11:49

+0

你想让你的类可以像android视图一样在XML中使用吗? – dreamtale 2012-03-22 12:12:14

回答

0

如果你想添加一个项目到你的视图,你总是可以夸大它。

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

View view = inflater.inflate(R.layout.ABC, null); 
addView(view); 
0

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

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

这是你需要什么?