質問

以下からカスタム ビューの使用について学んでいます。

http://developer.android.com/guide/topics/ui/custom-components.html#modifying

説明には次のように書かれています。

クラスの初期化いつものように、スーパーは最初に呼ばれます。その上 これはデフォルトのコンストラクタではありませんが、 パラメーター化されたもの。EditText は これらのパラメータを使用して作成され、 は XML レイアウト ファイルからインフレートされ、 したがって、コンストラクタは それらを取り、 スーパークラスコンストラクタも同様です。

もっと良い説明はありますか?私はコンストラクターがどのようなものであるべきかを理解しようとしており、4 つの可能な選択肢を思いつきました (投稿の最後にある例を参照)。これら 4 つの選択肢が何をするのか (またはしないのか)、なぜそれらを実装する必要があるのか​​、パラメーターが何を意味するのかがわかりません。これらについての説明はありますか?

public MyCustomView()
{
    super();
}

public MyCustomView(Context context)
{
    super(context);
}

public MyCustomView(Context context, AttributeSet attrs)
{
    super(context, attrs);
} 

public MyCustomView(Context context, AttributeSet attrs, Map params)
{
    super(context, attrs, params);
} 
役に立ちましたか?

解決

最初のものは機能しないので必要ありません。

3番目はあなたのカスタムを意味します View XML レイアウト ファイルから使用できるようになります。それを気にしないなら必要ありません。

私の知る限り、4番目は単に間違っています。ありません View を受け取るコンストラクター Map 3 番目のパラメータとして。かかるものがあります int 3 番目のパラメータとして、ウィジェットのデフォルトのスタイルをオーバーライドするために使用されます。

私がよく使うのは、 this() これらを組み合わせる構文:

public ColorMixer(Context context) {
    this(context, null);
}

public ColorMixer(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public ColorMixer(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // real work here
}

このコードの残りの部分は次の場所で確認できます。 この本の例.

他のヒント

これが私のパターンです(カスタムの作成) ViewGoup ここにありますが、それでも):

// CustomView.java

public class CustomView extends LinearLayout {

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

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context ctx) {
        LayoutInflater.from(ctx).inflate(R.layout.view_custom, this, true);
            // extra init
    }

}

そして

// view_custom.xml

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Views -->
</merge>

カスタムを追加するとき View から xml のように :

 <com.mypack.MyView
      ...
      />

が必要になります パブリックコンストラクター MyView(Context context, AttributeSet attrs), それ以外の場合は、 Exception いつ Android 努める inflate あなたの View.

そして、あなたがあなたのものを追加すると、 View から xml そしてまた 特定android:style attribute のように :

 <com.mypack.MyView
      style="@styles/MyCustomStyle"
      ...
      />

3番目も必要になります パブリックコンストラクター MyView(Context context, AttributeSet attrs,int defStyle) .

3 番目のコンストラクターは通常、スタイルを拡張してカスタマイズし、それを設定する場合に使用されます。 style 与えられたものに View あなたのレイアウトで

詳細を編集する

public MyView(Context context, AttributeSet attrs) {
            //Called by Android if <com.mypack.MyView/> is in layout xml file without style attribute.
            //So we need to call MyView(Context context, AttributeSet attrs, int defStyle) 
            // with R.attr.customViewStyle. Thus R.attr.customViewStyle is default style for MyView.
            this(context, attrs, R.attr.customViewStyle);
    }

これを参照してください

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top