zoukankan      html  css  js  c++  java
  • 自定义控件的使用方法

    1.自定义一个派生的view或者viewgroup类的控件时,必须实现一个构造函数,有三个构造函数需要实现

    如:

       public CustomView(Context context) {
            super(context);
           
        }
        public CustomView(Context context, @Nullable AttributeSet attrs) {
            super(context, attrs);
           
        }
        public CustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {//xml引用控件,必须实现这个构造函数
            super(context, attrs, defStyleAttr);
           
        }

    动态添加控件

            /**
             * layout_wight定义
             */
    //        LinearLayout rootView = (LinearLayout) findViewById(R.id.root);
    //        CustomView customView = new CustomView(this);
    //        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
    //        layoutParams.weight = 1.0f;
    //        rootView.addView(customView, layoutParams);
    
    
            /**
             * RelativeLayout添加布局规则
             */
    //        RelativeLayout rootView = (RelativeLayout) findViewById(R.id.root);
    //        CustomView customView = new CustomView(this);
    //        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    //        layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.text);  //第一个参数是相对布局的布局属性,第二个参数是指相对哪个控件ID来布局
    //        rootView.addView(customView, layoutParams);
    
    
            /**
             * setGravity使用
             */
            RelativeLayout rootView = (RelativeLayout) findViewById(R.id.root);
            RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200);
            layoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.text);
    
            Button button = new Button(this);
            button.setGravity(Gravity.TOP);
            button.setText("btn");
            rootView.addView(button, layoutParams);
            rootView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

    RelativeLayoutLinearLayout特殊,需要多设置相对属性在代码中动态设置,这些的方法是通过RelativeLayout.LayiutParams的addRule函数

    public void addRule(int verb, int subject)// verb布局属性,subject根据哪个控件ID布局
  • 相关阅读:
    WIN7远程桌面连接--“发生身份验证错误。要求的函数不受支持”
    django-xadmin使用之更改菜单url
    django-xadmin使用之配置页眉页脚
    django-xadmin定制之列表页searchbar placeholder
    django-xadmin定制之分页显示数量
    Chrome无界面浏览模式与自定义插件加载问题
    Chrome开启无界面浏览模式Python+Windows环境
    django-xadmin中APScheduler的启动初始化
    处理nginx访问日志,筛选时间大于1秒的请求
    将Excel文件转为csv文件的python脚本
  • 原文地址:https://www.cnblogs.com/loaderman/p/10191771.html
Copyright © 2011-2022 走看看