0

ビューを拡張するクラスで XML レイアウト ファイルを呼び出したいと考えています。アクティビティを拡張しないため、その中で oncreate 関数を実行できません。私のイベントは 1 つのクラス ファイルにあり、別のクラス ファイルでそのクラスを呼び出しています。しかし、これらのイベントを 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

ビューに項目を追加したい場合は、いつでもそれを膨らませることができます。

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 に答える
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 に答える