自定义控件的小总结

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jakezhang1990/article/details/82703597

自定义控件,总的来说有3种方式,自绘控件,继承控件,组合式控件

  1. 自绘控件
    主要是使用Paint画笔和Cavas画布,进行绘制;

  2. 继承控件
    主要是继承某一父类,然后在此基础上,定义自己想要的效果和功能;比如垂直的进度条,可以继承普通的ProgressBar然后进行方向的旋转设置,原本进度条的方法依然可以继续使用。
    其实所有的控件都继承自view类,严格来说这种方式使用最多。

  3. 组合式控件
    这种方式有点意思,是通过一个xml设置属性attr,在这个xml中进行属性的设置。

在实现复杂自定义控件时,往往3种方式混合使用,更棒~

今天着重分析一波这个组合式控件,实现自定义。
现在呢,要定义一个自定义一个actionbar控件。就简单的两个部分,一个是左侧的返回箭头,一个是中间的标题;希望可以使用一个控件就完成这-功能,而不用每次都去写一个布局容器,左侧放ImageView,中间放TextView来实现这个功能。
1,在value文件夹中定义一个attr.xml文件,在里面定义一个属性簇。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="CustomTopTitleView">
        <!--标题显示类型-->
        <attr name="back_text_type" format="string" />
    </declare-styleable>
</resources>

2,控件的java类,可以继承相对布局。

public class CustomTopTitleView extends RelativeLayout {
    private ImageView mSrcLogo;//图标log
    private TextView mTextTitle;//标题

    public CustomTopTitleView(Context context) {
        super(context);
    }

    public CustomTopTitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
        initAttrs(context, attrs);
    }

    public CustomTopTitleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
        initAttrs(context, attrs);
    }

    private void initView(Context context) {
        View textView = LayoutInflater.from(context).inflate(R.layout.custom_top_title_layout, this);

        mSrcLogo = textView.findViewById(R.id.src_back_ivw);
        mTextTitle = textView.findViewById(R.id.text_back_tvw);
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTopTitleView);

        String titleType = typedArray.getString(R.styleable.CustomTopTitleView_back_text_type);//标题显示类型

        typedArray.recycle();
        setDefinedView(titleType);
    }

    private void setDefinedView(String titleType) {
        mTextTitle.setText(titleType);
    }

    /**
     * 返回事件
     *
     * @param listener
     */
    public void toBackReturn(OnClickListener listener) {
        mSrcLogo.setOnClickListener(listener);
    }

    public void setTopText(String text) {
        mTextTitle.setText(text);
    }

3,在需要添加actionbar的布局中,使用该控件。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <com.xx.CustomTopTitleView
        android:id="@+id/customTopTitleView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        custom:back_text_type="这是标题栏" />

</RelativeLayout>

可以看到,这里有了custom:back_text_type="这是标题栏"这个属性,这个属性的名字,就是在attr.xml中命名的。

猜你喜欢

转载自blog.csdn.net/jakezhang1990/article/details/82703597