Android 自定义组合控件

Android 自定义组合控件_第1张图片
Paste_Image.png
  • 自定义组合控件

  • 第一步定义你要复用的布局




    

    

    

    

    

    


  • 第二步定义自定义属性

比如标题文字、标题栏左边按钮图标等。


        
        
        
        
        
        
 
  • 第三步自定义一个View根据需求继承不同的ViewGroup子类,比如:RelativeLayout、LinearLayout等,我们这里继承RelativeLayout
public class CommonValueItemView extends RelativeLayout {

初始化控件

private void initView(Context context) {
        View.inflate(context, R.layout.view_common_value_item, this);
        iv = (ImageView) this.findViewById(R.id.iv);
        tv_title = (TextView) this.findViewById(R.id.tv_title);
        tv_value = (TextView) this.findViewById(R.id.tv_value);
        divider_line=findViewById(R.id.divider_line);
        iv_next=(ImageView) findViewById(R.id.iv_next);
        iv_new=(ImageView) findViewById(R.id.iv_new);
    }

重写构造方法 ,在构造方法中初始化控件,并引用自定义属性

public CommonValueItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonValueItemView);
        String title = attributes.getString(R.styleable.CommonValueItemView_item_title);
        setTitle(title);
        //设置item之间分隔线是否显示
        boolean visibility = attributes.getBoolean(R.styleable.CommonValueItemView_divider_line_visibility, true);
        if (visibility) {
            setDividerLineVisibility(View.VISIBLE);
        } else {
            setDividerLineVisibility(View.GONE);
        }
        //获取是否要显示左边图片
        int leftImgResource = attributes.getResourceId(R.styleable.CommonValueItemView_item_img, -1);
        if (leftImgResource != -1) {
            setIvResource(leftImgResource);
        } else {
            iv.setVisibility(View.GONE);
        }
        //获取是否要显示右边按钮
        boolean nextVisibiliy = attributes.getBoolean(R.styleable.CommonValueItemView_next_visibility, true);
        if (nextVisibiliy) {
            iv_next.setVisibility(View.INVISIBLE);
        } else {
            iv_next.setVisibility(View.GONE);
        }
        //设置值是否显示
        boolean valueVisibility = attributes.getBoolean(R.styleable.CommonValueItemView_value_visibility, true);
        if (valueVisibility) {
            setValueVisibility(View.VISIBLE);
        } else {
            setValueVisibility(View.GONE);
        }
    }
  • 完整代码
public class CommonValueItemView extends RelativeLayout {
    
    private ImageView iv;
    private TextView tv_title;
    private TextView tv_value;
    private View divider_line;
    private ImageView iv_next;
    private ImageView iv_new;

    private void initView(Context context) {
        View.inflate(context, R.layout.view_common_value_item, this);
        iv = (ImageView) this.findViewById(R.id.iv);
        tv_title = (TextView) this.findViewById(R.id.tv_title);
        tv_value = (TextView) this.findViewById(R.id.tv_value);
        divider_line=findViewById(R.id.divider_line);
        iv_next=(ImageView) findViewById(R.id.iv_next);
        iv_new=(ImageView) findViewById(R.id.iv_new);
    }
    
    public CommonValueItemView(Context context) {
        super(context);
        initView(context);
    }

    public CommonValueItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.CommonValueItemView);
        String title = attributes.getString(R.styleable.CommonValueItemView_item_title);
        setTitle(title);
        //设置item之间分隔线是否显示
        boolean visibility = attributes.getBoolean(R.styleable.CommonValueItemView_divider_line_visibility, true);
        if (visibility) {
            setDividerLineVisibility(View.VISIBLE);
        } else {
            setDividerLineVisibility(View.GONE);
        }
        //获取是否要显示左边图片
        int leftImgResource = attributes.getResourceId(R.styleable.CommonValueItemView_item_img, -1);
        if (leftImgResource != -1) {
            setIvResource(leftImgResource);
        } else {
            iv.setVisibility(View.GONE);
        }
        //获取是否要显示右边按钮
        boolean nextVisibiliy = attributes.getBoolean(R.styleable.CommonValueItemView_next_visibility, true);
        if (nextVisibiliy) {
            iv_next.setVisibility(View.INVISIBLE);
        } else {
            iv_next.setVisibility(View.GONE);
        }
        //设置值是否显示
        boolean valueVisibility = attributes.getBoolean(R.styleable.CommonValueItemView_value_visibility, true);
        if (valueVisibility) {
            setValueVisibility(View.VISIBLE);
        } else {
            setValueVisibility(View.GONE);
        }
    }

    public CommonValueItemView(Context context, AttributeSet attrs,
            int defStyle) {
        super(context, attrs, defStyle);
        initView(context);
    }
    
    public void setIvResource(int resId){
        iv.setImageResource(resId);
    }
    
    public void setTitle(int resid){
        tv_title.setText(resid);
    }
    public void setTitle(String resid){
        tv_title.setText(resid);
    }
    
    public void setValue(int resid){
        tv_value.setText(resid);
    }
    public void setValue(String resid){
        tv_value.setText(resid);
    }
    
    public String getValue(){
        return tv_value.getText().toString();
    }
    
    public void setValueVisibility(int visibility){
        tv_value.setVisibility(visibility);
    }
    public void setDividerLineVisibility(int visibility){
        divider_line.setVisibility(visibility);
    }

    public void setNextVisibility(int visibility){
        if(View.VISIBLE==visibility){
            iv_new.setVisibility(View.GONE);
        }
        iv_next.setVisibility(visibility);
    }
    
    public void setNewVisibility(int visibility){
        if(View.VISIBLE==visibility){
            iv_next.setVisibility(View.GONE);
        }
        iv_new.setVisibility(visibility);
    }
}
  • 最后一步在需要的xml中进行引用并在根结点添加xmlns:app="http://schemas.android.com/apk/res-auto"

            

自定义组合控件能够提高开发效率,降低维护成本

你可能感兴趣的:(Android 自定义组合控件)