2012-03-22 11 views
0

ビューを拡張するクラスでXMLレイアウトファイルを呼び出すとします。私はそれが活動を拡張しないので、oncreate機能を実行できません。私のイベントは1つのクラスファイルにあり、私は別のクラスファイルでそのクラスを呼び出しています。 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

+0

あなたのクラスはXMLでアンドロイドのように使用できますか? – dreamtale

答えて

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);

が、これは何が必要でしょうか?

関連する問題