组合型自定义控件

自定义控件可以分为三种:

 1.完全自定义绘制+事件,动画的逻辑处理 

 2.继承某个ViweGrop或者View+事件、动画的逻辑处理

 3.组合型自定义控件(一般只是点击事件,隐藏显示,文字,等等逻辑处理) 。

  第三种是最简单的,在开发中也是非常实用的,可以省去很多麻烦,节省时间。今天就是对它进行一个总结,下面以自定义布局显示框为例进行讲解。

步骤一:首先写一个布局,能实现自己想要的功能的布局。直接上代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/ll_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:background="#f1cdcd"
    android:gravity="center_vertical">
        <TextView
            android:id="@+id/textview1_left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="我的余额"
            android:textColor="@color/text_black"
            android:textSize="14sp" />

        <LinearLayout
            android:id="@+id/ll"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:orientation="horizontal"
           >
            <TextView
                android:id="@+id/textview1_right"
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="match_parent"
                android:text="300元"
                android:gravity="right|center_vertical"
                android:textColor="@color/common_black_text_color"
                android:textSize="14sp" />
            <ImageView
                android:id="@+id/textview1_right_arrow"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:paddingLeft="4dp"
                android:gravity="right|center_vertical"
                android:layout_gravity="center|right"
                android:src="@drawable/input_right_arrow" />
        </LinearLayout>
    </LinearLayout>

步骤二:根据自己的功能情况,定义一些可以直接在布局中设置的属性。 此例子中我进行 左边文字,颜色属性,右边的文字 颜色属性等,右边的图片,是否隐藏等 属性,在此不一一列举 。创建attrs.xml文件,然后定义一系列属性,如下:

<declare-styleable name="MyTextView1">
    <attr name="textview1_llBackground" format="color|reference" />
    <attr name="textview1_textSize" format="integer" />
    <attr name="textview1_contentLeft" format="string" />
    <attr name="textview1_contentRight" format="string" />
    <attr name="textview1_colorLeft" format="color|reference" />
    <attr name="textview1_colorRight" format="color|reference" />
    <attr name="textview1_arrowRight" format="reference|color" />
    <attr name="textview1_visibleArrow" format="boolean" />
</declare-styleable>

步骤三 :写一个定义ViewGroup关联上面的布局,并对其进行初始化操作以及对外的一些逻辑方法。

/**
 * Created by liukang on 2018/1/12 13:52.
 * 可选择的TextView
 * TextView :    我的余额      100元  >
 */

public class MyTextView1 extends LinearLayout {

    /**
     * 控件
     */
    LinearLayout linearLayout;
    TextView textViewLeft;
    TextView textViewRight;
    ImageView imageViewArrow;

    /**
     * 属性
     *
     * @param context
     */

    private Drawable llBackground;
    private int textSize;
    private boolean visibleArrow;
    private String contentLeft;
    private String contentRight;
    private int colorLeft;
    private int colorRight;
    private Drawable arrowRight;

    Context context;

    public MyTextView1(Context context) {
        this(context, null);
        initView();
    }

    public MyTextView1(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public MyTextView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        LayoutInflater.from(context).inflate(R.layout.mytextview1_layout, this, true);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyTextView1);
        initTypeArray(typedArray);
        initView();
    }

    private void initTypeArray(TypedArray typedArray) {
        llBackground = typedArray.getDrawable(R.styleable.MyTextView1_textview1_llBackground);
        textSize = typedArray.getInt(R.styleable.MyTextView1_textview1_textSize, 14);
        visibleArrow = typedArray.getBoolean(R.styleable.MyTextView1_textview1_visibleArrow, false);
        contentLeft = typedArray.getString(R.styleable.MyTextView1_textview1_contentLeft);
        contentRight = typedArray.getString(R.styleable.MyTextView1_textview1_contentRight);
        colorLeft = typedArray.getColor(R.styleable.MyTextView1_textview1_colorLeft,      context.getResources().getColor(R.color.common_black_text_color)); 
        colorRight = typedArray.getColor(R.styleable.MyTextView1_textview1_colorRight, context.getResources().getColor(R.color.common_black_text_color));
        arrowRight = typedArray.getDrawable(R.styleable.MyTextView1_textview1_arrowRight);
        typedArray.recycle();
    }

    private void initView() {
        /**
         * 初始化参数
         */
        linearLayout=findViewById(R.id.ll_root);
        textViewLeft = findViewById(R.id.textview1_left);
        textViewRight = findViewById(R.id.textview1_right);
        imageViewArrow = findViewById(R.id.textview1_right_arrow);

        linearLayout.setBackground(llBackground);
        //字体大小
        textViewLeft.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        textViewRight.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);

        //左边内容+颜色   右边内容+颜色
        textViewLeft.setText(contentLeft);
        textViewLeft.setTextColor(colorLeft);
        textViewRight.setText(contentRight);
        textViewRight.setTextColor(colorRight);

        if (arrowRight != null) {
            imageViewArrow.setImageDrawable(arrowRight);
        }
        //是否显示右箭头
        if (visibleArrow) {
            imageViewArrow.setVisibility(VISIBLE);
        } else {
            imageViewArrow.setVisibility(GONE);
        }

    }

    /**
     * 手动进行代码设置
     * @param s
     */
    public void setLeftContent(String s) {
        textViewLeft.setText(s);
    }

    public void setRightContent(String s) {
        textViewRight.setText(s);
    }

    public void setLeftColor(String color) {
        textViewLeft.setTextColor(Color.parseColor(color));
    }

    public void setRightColor(String color) {
        textViewRight.setTextColor(Color.parseColor(color));
    }

    /**
     * 手动获取
     * @return
     */
    public String getLeftContent() {
        String trim = textViewLeft.getText().toString().trim();
        return trim == null ? "" : trim;
    }

    public String getRightContent() {
        String trim = textViewRight.getText().toString().trim();
        return trim == null ? "" : trim;
    }


    /**
     * 右箭头
     * @param resId
     */
    public void setRightArrow(int resId) {
        imageViewArrow.setImageResource(resId);
    }

    public void setArrowVisible(boolean b) {
        if (b) {
            imageViewArrow.setVisibility(VISIBLE);
        } else {
            imageViewArrow.setVisibility(GONE);
        }
    }

    public void setRightArrowListener(OnClickListener listener) {
        ( (View)imageViewArrow.getParent()).setOnClickListener(listener);
    }
}

总结:这种组合型自定义控件是比较简单的,也是很实用的,比如TitleBar, APP中经常出现的相同的布局等,使用起来一个控件就搞定,Activity代码里面又可以动态的设置,就这些吧。

猜你喜欢

转载自blog.csdn.net/lk2021991/article/details/85011935
今日推荐