Custom view made of multiple views

前端 未结 3 1721
长情又很酷
长情又很酷 2020-12-08 20:42

I have a set of views I want to always use together. An example of this may be something like:


    
    

        
3条回答
  •  感动是毒
    2020-12-08 21:03

    This example is for a horizontal number picker widget, but it's the same concept.

    First create the XML layout for your custom component

    
    
    
       

    Then create the java class

    public class HorizontalNumberPicker extends LinearLayout {
        public HorizontalNumberPicker(Context context, AttributeSet attrs) {
             super(context, attrs);
             LayoutInflater inflater = LayoutInflater.from(context);
             inflater.inflate(R.layout.horizontal_number_picker, this);
         }
     }
    

    Add whatever logic you need to that java class, then you can just include the custom component in your XML layout like so:

    
    


    Check out this link for more information: http://developer.android.com/guide/topics/ui/custom-components.html#compound

提交回复
热议问题