Android Custom View Constructor

后端 未结 3 1463
独厮守ぢ
独厮守ぢ 2020-11-27 15:16

I\'m learning about using Custom Views from the following:

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

The description says

相关标签:
3条回答
  • 2020-11-27 15:53

    You don't need the first one, as that just won't work.

    The third one will mean your custom View will be usable from XML layout files. If you don't care about that, you don't need it.

    The fourth one is just wrong, AFAIK. There is no View constructor that take a Map as the third parameter. There is one that takes an int as the third parameter, used to override the default style for the widget.

    I tend to use the this() syntax to combine these:

    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
    }
    

    You can see the rest of this code in this book example.

    0 讨论(0)
  • 2020-11-27 15:56

    When you are adding your custom View from xml like :

     <com.mypack.MyView
          ...
          />
    

    you will need the public constructor MyView(Context context, AttributeSet attrs), otherwise you will get an Exception when Android tries to inflate your View.

    And when you add your View from xml and also specify the android:style attribute like :

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

    you will also need the third public constructor MyView(Context context, AttributeSet attrs,int defStyle) .

    The third constructor is usually used when you extend a style and customize it, and then you would like to set that style to a given View in your layouts

    Edit Details

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

    See this

    0 讨论(0)
  • 2020-11-27 16:08

    Here's a my pattern (creating a custom ViewGoup here, but still):

    // 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
        }
    
    }
    

    and

    // view_custom.xml
    
    <merge xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Views -->
    </merge>
    
    0 讨论(0)
提交回复
热议问题