2012-03-22 114 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);

這是你需要什麼?