首页

android自定义控件例子源码下载

java

2020-7-30

package scratchcard.cbt.com.learnuserdefinedview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * Created by caobotao on 15/12/9.
 */
public class TopBar extends RelativeLayout{
    /*
     * 声明控件
     */
    private Button leftBtn;//左按钮
    private TextView textView;//中间文本框
    private Button rightBtn;//右按钮

    /*
     * 声明三个控件的属性
     */
    //声明左按钮的属性
    private String leftText;//按钮文本
    private Drawable leftBackground;//按钮背景
    private int leftTextColor;//按钮文本颜色
    //声明中间文本框的属性
    private String tbTitle;//文本框文本
    private float titleTextSize;//文本框字体大小
    private int titleTextColor;//文本框字体颜色
    //声明右按钮的属性
    private String rightText;//按钮文本
    private Drawable rightBackground;//按钮背景
    private int rightTextColor;//按钮文本颜色

    /*
     * 声明三个控件的布局属性
     */
    private LayoutParams leftBtnLayoutParams;
    private LayoutParams textViewLayoutParams;
    private LayoutParams rightBtnLayoutParams;

    //声明左右按钮点击监听
    private TopBarBtnsOnClickListener listener;

    //创建一个监听左右按钮点击的接口
    public interface TopBarBtnsOnClickListener{
        public void leftBtnOnClick();//左按钮被点击的事件
        public void rightBtnOnClick();//右按钮被点击的事件
    }

    //向外提供一个设置监听的方法
    public void setOnTopBarBtnsClick(TopBarBtnsOnClickListener listener){
        this.listener = listener;
    }

    //重写构造方法
    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        /*
         * 用TypedArray可以获取用户在xml中声明的此控件的所有属性,以键值对存储,
         * K:资源文件(例 R.styleable.topBar_leftText)
         * V:属性值
         */
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.topBar);

        //为左按钮的属性赋值
        leftText = ta.getString(R.styleable.topBar_leftText);
        leftBackground = ta.getDrawable(R.styleable.topBar_leftBackground);
        leftTextColor = ta.getInt(R.styleable.topBar_leftTextColor,0);

        //为中间的文本框的属性赋值
        tbTitle = ta.getString(R.styleable.topBar_tbTitle);
        titleTextSize = ta.getDimension(R.styleable.topBar_titleTextSize,0);
        titleTextColor = ta.getInt(R.styleable.topBar_titleTextColor,0);

        //为右按钮的属性赋值
        rightText = ta.getString(R.styleable.topBar_rightText);
        rightBackground = ta.getDrawable(R.styleable.topBar_rightBackground);
        rightTextColor = ta.getInt(R.styleable.topBar_rightTextColor,0);

        //使用完TypedArray之后需要调用其recycle()方法,以便重用
        ta.recycle();

        //实例化三个控件
        leftBtn = new Button(context);
        textView = new TextView(context);
        rightBtn = new Button(context);

        //设置左按钮的属性
        leftBtn.setText(leftText);
        leftBtn.setBackground(leftBackground);
        leftBtn.setTextColor(leftTextColor);
        //设置文本框的属性
        textView.setText(tbTitle);
        textView.setTextSize(titleTextSize);
        textView.setTextColor(titleTextColor);
        textView.setGravity(Gravity.CENTER);
        //设置右按钮的属性
        rightBtn.setText(rightText);
        rightBtn.setBackground(rightBackground);
        rightBtn.setTextColor(rightTextColor);

        //设置此自定义控件的背景颜色
        setBackgroundColor(0xFFF59563);

        //实例化左按钮的布局属性
        leftBtnLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        //设置左按钮靠左显示
        leftBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        //将左按钮添加到本自定义控件中
        addView(leftBtn,leftBtnLayoutParams);

        //同上
        rightBtnLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        rightBtnLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        addView(rightBtn,rightBtnLayoutParams);

        //同上
        textViewLayoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(textView,textViewLayoutParams);

        //回调左按钮的监听事件
        leftBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.leftBtnOnClick();
            }
        });

        //回调右按钮的监听事件
        rightBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.rightBtnOnClick();
            }
        });

    }

    //当然,我们还可以添加其他控制此控件的方法,如设置左按钮是否可见等等,大家可根据自己的需求进行扩展
    public void setLeftBtnVisible(boolean isVisible){
        leftBtn.setVisibility(isVisible ? VISIBLE : INVISIBLE);
    }
}
资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
app/.gitignore , 7
app/app.iml , 7500
app/build/generated/source/buildConfig/androidTest/debug/scratchcard/cbt/com/learnuserdefinedview/test/BuildConfig.java , 497
app/build/generated/source/buildConfig/debug/scratchcard/cbt/com/learnuserdefinedview/BuildConfig.java , 487
app/build/generated/source/r/debug/android/support/v7/appcompat/R.java , 81056
app/build/generated/source/r/debug/scratchcard/cbt/com/learnuserdefinedview/R.java , 379955
app/build/intermediates/blame/res/debug/multi/color.json , 2
app/build/intermediates/blame/res/debug/multi/values-af.json , 10160
app/build/intermediates/blame/res/debug/multi/values-am.json , 10153
app/build/intermediates/blame/res/debug/multi/values-ar.json , 10162
app/build/intermediates/blame/res/debug/multi/values-bg.json , 10164
app/build/intermediates/blame/res/debug/multi/values-bn-rBD.json , 10278
app/build/intermediates/blame/res/debug/multi/values-ca.json , 10164
app/build/intermediates/blame/res/debug/multi/values-cs.json , 10162
app/build/intermediates/blame/res/debug/multi/values-da.json , 10158
app/build/intermediates/blame/res/debug/multi/values-de.json , 10160
app/build/intermediates/blame/res/debug/multi/values-el.json , 10166
app/build/intermediates/blame/res/debug/multi/values-en-rGB.json , 10270
app/build/intermediates/blame/res/debug/multi/values-en-rIN.json , 10270
app/build/intermediates/blame/res/debug/multi/values-es-rUS.json , 10278
app/build/intermediates/blame/res/debug/multi/values-es.json , 10162
app/build/intermediates/blame/res/debug/multi/values-et-rEE.json , 10276
app/build/intermediates/blame/res/debug/multi/values-eu-rES.json , 10274
app/build/intermediates/blame/res/debug/multi/values-fa.json , 10162
app/build/intermediates/blame/res/debug/multi/values-fi.json , 10158
app/build/intermediates/blame/res/debug/multi/values-fr-rCA.json , 10278
app/build/intermediates/blame/res/debug/multi/values-fr.json , 10166
app/build/intermediates/blame/res/debug/multi/values-gl-rES.json , 10276
app/build/intermediates/blame/res/debug/multi/values-h720dp-v13.json , 976
app/build/intermediates/blame/res/debug/multi/values-hdpi-v4.json , 1038
app/build/intermediates/blame/res/debug/multi/values-hi.json , 10161
app/build/intermediates/blame/res/debug/multi/values-hr.json , 10162
app/build/intermediates/blame/res/debug/multi/values-hu.json , 10164
app/build/intermediates/blame/res/debug/multi/values-hy-rAM.json , 10274
app/build/intermediates/blame/res/debug/multi/values-in.json , 10164
app/build/intermediates/blame/res/debug/multi/values-is-rIS.json , 10268
app/build/intermediates/blame/res/debug/multi/values-it.json , 10161
app/build/intermediates/blame/res/debug/multi/values-iw.json , 10157
app/build/intermediates/blame/res/debug/multi/values-ja.json , 10148
app/build/intermediates/blame/res/debug/multi/values-ka-rGE.json , 10275
app/build/intermediates/blame/res/debug/multi/values-kk-rKZ.json , 10273
app/build/intermediates/blame/res/debug/multi/values-km-rKH.json , 10272
app/build/intermediates/blame/res/debug/multi/values-kn-rIN.json , 10276
app/build/intermediates/blame/res/debug/multi/values-ko.json , 10144
app/build/intermediates/blame/res/debug/multi/values-ky-rKG.json , 10272
app/build/intermediates/blame/res/debug/multi/values-land.json , 5574
app/build/intermediates/blame/res/debug/multi/values-large-v4.json , 8690
app/build/intermediates/blame/res/debug/multi/values-lo-rLA.json , 10270
app/build/intermediates/blame/res/debug/multi/values-lt.json , 10164
app/build/intermediates/blame/res/debug/multi/values-lv.json , 10164
app/build/intermediates/blame/res/debug/multi/values-mk-rMK.json , 9497
app/build/intermediates/blame/res/debug/multi/values-ml-rIN.json , 10276
app/build/intermediates/blame/res/debug/multi/values-mn-rMN.json , 10272
app/build/intermediates/blame/res/debug/multi/values-mr-rIN.json , 10275
app/build/intermediates/blame/res/debug/multi/values-ms-rMY.json , 10274
app/build/intermediates/blame/res/debug/multi/values-my-rMM.json , 10278
app/build/intermediates/blame/res/debug/multi/values-nb.json , 10154
app/build/intermediates/blame/res/debug/multi/values-ne-rNP.json , 10278
app/build/intermediates/blame/res/debug/multi/values-nl.json , 10164
app/build/intermediates/blame/res/debug/multi/values-pl.json , 10164
app/build/intermediates/blame/res/debug/multi/values-port.json , 960
app/build/intermediates/blame/res/debug/multi/values-pt-rPT.json , 10278
app/build/intermediates/blame/res/debug/multi/values-pt.json , 10166
app/build/intermediates/blame/res/debug/multi/values-ro.json , 10164
app/build/intermediates/blame/res/debug/multi/values-ru.json , 10164
app/build/intermediates/blame/res/debug/multi/values-si-rLK.json , 10273
app/build/intermediates/blame/res/debug/multi/values-sk.json , 10160
app/build/intermediates/blame/res/debug/multi/values-sl.json , 10163
app/build/intermediates/blame/res/debug/multi/values-sr.json , 10164
app/build/intermediates/blame/res/debug/multi/values-sv.json , 10160
app/build/intermediates/blame/res/debug/multi/values-sw.json , 10160
app/build/intermediates/blame/res/debug/multi/values-sw600dp-v13.json , 7175
app/build/intermediates/blame/res/debug/multi/values-ta-rIN.json , 10276
app/build/intermediates/blame/res/debug/multi/values-te-rIN.json , 10276
app/build/intermediates/blame/res/debug/multi/values-th.json , 10160
app/build/intermediates/blame/res/debug/multi/values-tl.json , 10166
app/build/intermediates/blame/res/debug/multi/values-tr.json , 10160
app/build/intermediates/blame/res/debug/multi/values-uk.json , 10162
app/build/intermediates/blame/res/debug/multi/values-ur-rPK.json , 10276
app/build/intermediates/blame/res/debug/multi/values-uz-rUZ.json , 9497
app/build/intermediates/blame/res/debug/multi/values-v11.json , 14257
app/build/intermediates/blame/res/debug/multi/values-v12.json , 3399
app/build/intermediates/blame/res/debug/multi/values-v14.json , 8178
app/build/intermediates/blame/res/debug/multi/values-v17.json , 11999
app/build/intermediates/blame/res/debug/multi/values-v18.json , 955
app/build/intermediates/blame/res/debug/multi/values-v21.json , 67457
app/build/intermediates/blame/res/debug/multi/values-vi.json , 10166
app/build/intermediates/blame/res/debug/multi/values-w360dp-v13.json , 976
app/build/intermediates/blame/res/debug/multi/values-w480dp-v13.json , 1747
app/build/intermediates/blame/res/debug/multi/values-w500dp-v13.json , 976
app/build/intermediates/blame/res/debug/multi/values-w600dp-v13.json , 1747
app/build/intermediates/blame/res/debug/multi/values-w720dp-v13.json , 976
app/build/intermediates/blame/res/debug/multi/values-w820dp-v13.json , 905
app/build/intermediates/blame/res/debug/multi/values-xlarge-land-v4.json , 994
app/build/intermediates/blame/res/debug/multi/values-xlarge-v4.json , 5603
app/build/intermediates/blame/res/debug/multi/values-zh-rCN.json , 10254
app/build/intermediates/blame/res/debug/multi/values-zh-rHK.json , 10254
app/build/intermediates/blame/res/debug/multi/values-zh-rTW.json , 10254
app/build/intermediates/blame/res/debug/multi/values-zu.json , 10164
app/build/intermediates/blame/res/debug/multi/values.json , 360747
app/build/intermediates/blame/res/debug/single/anim.json , 3508
app/build/intermediates/blame/res/debug/single/color-v11.json , 844
app/build/intermediates/blame/res/debug/single/color.json , 3416
app/build/intermediates/blame/res/debug/single/drawable-hdpi-v4.json , 15503
app/build/intermediates/blame/res/debug/single/drawable-ldrtl-hdpi-v17.json , 1616
app/build/intermediates/blame/res/debug/single/drawable-ldrtl-mdpi-v17.json , 1616
app/build/intermediates/blame/res/debug/single/drawable-ldrtl-xhdpi-v17.json , 1624
app/build/intermediates/blame/res/debug/single/drawable-ldrtl-xxhdpi-v17.json , 1632
app/build/intermediates/blame/res/debug/single/drawable-ldrtl-xxxhdpi-v17.json , 1640
app/build/intermediates/blame/res/debug/single/drawable-mdpi-v4.json , 15503
app/build/intermediates/blame/res/debug/single/drawable-xhdpi-v4.json , 15581
app/build/intermediates/blame/res/debug/single/drawable-xxhdpi-v4.json , 15659
app/build/intermediates/blame/res/debug/single/drawable-xxxhdpi-v4.json , 7599
app/build/intermediates/blame/res/debug/single/drawable.json , 7666
app/build/intermediates/blame/res/debug/single/layout.json , 14106
app/build/intermediates/blame/res/debug/single/mipmap-hdpi-v4.json , 291
app/build/intermediates/blame/res/debug/single/mipmap-mdpi-v4.json , 291
app/build/intermediates/blame/res/debug/single/mipmap-xhdpi-v4.json , 293
app/build/intermediates/blame/res/debug/single/mipmap-xxhdpi-v4.json , 295
app/build/intermediates/blame/res/debug/single/mipmap-xxxhdpi-v4.json , 297
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$anim.class , 813
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$attr.class , 8734
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$bool.class , 820
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$color.class , 3770
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$dimen.class , 4283
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$drawable.class , 3659
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$id.class , 3666
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$integer.class , 657
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$layout.class , 2313
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$string.class , 1430
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$style.class , 18523
app/build/intermediates/classes/debug/android/support/v7/appcompat/R$styleable.class , 18065
app/build/intermediates/classes/debug/android/support/v7/appcompat/R.class , 1019
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/BuildConfig.class , 777
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/MainActivity$1.class , 1182
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/MainActivity.class , 1472
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$anim.class , 849
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$attr.class , 9093
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$bool.class , 856
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$color.class , 3917
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$dimen.class , 4417
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$drawable.class , 3695
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$id.class , 3732
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$integer.class , 693
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$layout.class , 2386
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$mipmap.class , 467
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$string.class , 1498
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$style.class , 18591
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R$styleable.class , 18582
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/R.class , 1259
app/build/intermediates/classes/debug/scratchcard/cbt/com/learnuserdefinedview/TopBar$1.class , 1181
最多只能显示150条信息!
没有账号? 忘记密码?

社交账号快速登录