一个简单的自定义View,可以显示上图下字

来源:互联网 发布:windows iso怎么下载 编辑:程序博客网 时间:2024/05/16 15:22


这个自定义View相对简单,主要是继承了相对布局,最底部的hintText 可以隐藏,当需要的时候才会显示;

上边的图只是显示了 四张自定义控件

<ImageButton        app:textSize="10sp"        app:src="@drawable/ic_vip"        app:text="充油卡"        app:srcHeight="30dp"        app:textColor="@color/hintTextColor"        android:layout_width="0dp"        android:layout_weight="1"        android:layout_height="wrap_content"/>

布局中引用

现在看看自定义属性

<declare-styleable name="ImageButton">        <!--引用显示的图片-->        <attr name="src" format="reference"/>        <!--显示的主要文本-->        <attr name="text" format="reference|string"/>        <!--设置图片的宽度-->        <attr name="srcWidth" format="reference"/>        <!--设置图片的高度-->        <attr name="srcHeight" format="reference|dimension"/>        <!--设置字体的高度-->        <attr name="textSize" format="dimension|reference"/>        <!--设置字体颜色-->        <attr name="textColor" format="color|reference"/>        <!--设置底部文字的颜色-->        <attr name="hintColor" format="color|reference"/>        <attr name="hintText" format="string|reference"/>        <attr name="hintTextSize" format="dimension|reference"/>    </declare-styleable>

下面就来看看里面的代码

public class ImageButton extends LinearLayout {    public static final int WRAP_CONTENT = -1;    public static final int MATCH_PARENT = -2;    private static final int TEXT_SIZE = 12;    private final static float HINT_TEXT_SIZE = 10;    private static final int PADDING_SIZE = 8;    private String text;    private int src;    private int srcWidth;    private int srcHeight;    private int textSize;    private int textColor;    private String hintText;    private int hintTextSize;    private int hintTextColor;    private ImageView mImageView;    private TextView mTextView;    private TextView mHintTextView;    private Paint mPaint;    public ImageButton(Context context) {        super(context);        init(context, null, 0);    }    public ImageButton(Context context, AttributeSet attrs) {        super(context, attrs);        init(context, attrs, 0);    }    public ImageButton(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init(context, attrs, defStyleAttr);    }    private void init(Context context, AttributeSet attrs, int defStyleAttr) {        this.setOrientation(LinearLayout.VERTICAL);        initData();        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ImageButton, defStyleAttr, 0);        int count = ta.getIndexCount();        for (int i = 0; i < count; i++) {            int attr = ta.getIndex(i);            switch (attr) {                case R.styleable.ImageButton_src:                    src = ta.getResourceId(attr, -1);                    break;                case R.styleable.ImageButton_text:                    text = ta.getString(attr);                    if (text == null) {                        int stringId = ta.getResourceId(attr, -1);                        if (stringId != -1) {                            text = getContext().getString(stringId);                        }                    }                    break;                case R.styleable.ImageButton_srcHeight:                    srcHeight = ta.getDimensionPixelOffset(attr, 0);                    break;                case R.styleable.ImageButton_srcWidth:                    srcWidth = ta.getDimensionPixelOffset(attr,0);                    break;                case R.styleable.ImageButton_textColor:                    textColor = ta.getColor(attr, textColor);                    break;                case R.styleable.ImageButton_textSize:                    textSize = (int) ta.getDimension(attr, TEXT_SIZE);                    break;                case R.styleable.ImageButton_hintColor:                    hintTextColor = ta.getColor(attr, hintTextColor);                    break;                case R.styleable.ImageButton_hintText:                    hintText = ta.getString(attr);                    break;                case R.styleable.ImageButton_hintTextSize:                    hintTextSize = (int) ta.getDimension(attr, -1);                    break;            }        }        ta.recycle();        mTextView = new TextView(getContext());        LinearLayout.LayoutParams tvLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        tvLp.gravity = Gravity.CENTER_HORIZONTAL;        mTextView.setSingleLine();        mTextView.setLayoutParams(tvLp);        mTextView.setTextColor(textColor);        if (text != null)            mTextView.setText(text);        mTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);        mImageView = new ImageView(getContext());        LinearLayout.LayoutParams ivLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        ivLp.weight = srcWidth;        ivLp.height = srcHeight;        ivLp.gravity = Gravity.CENTER_HORIZONTAL;        mImageView.setLayoutParams(ivLp);        mImageView.setImageResource(src);        LinearLayout.LayoutParams llLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        int padding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, PADDING_SIZE, getResources().getDisplayMetrics());        setPadding(padding, padding, padding, padding);        this.setLayoutParams(llLp);        mHintTextView = new TextView(getContext());        LinearLayout.LayoutParams hintTvLp = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);        hintTvLp.gravity = Gravity.CENTER_HORIZONTAL;        mHintTextView.setLayoutParams(hintTvLp);        if (hintText != null)            mHintTextView.setText(hintText);        mHintTextView.setTextSize(TypedValue.COMPLEX_UNIT_PX, hintTextSize);        mHintTextView.setTextColor(hintTextColor);        mHintTextView.setSingleLine();        this.addView(mImageView);        this.addView(mTextView);        this.addView(mHintTextView);    }    private void initData() {        textSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, TEXT_SIZE, getResources().getDisplayMetrics());        srcWidth = ViewGroup.LayoutParams.MATCH_PARENT;        srcHeight = ViewGroup.LayoutParams.MATCH_PARENT;        src = R.mipmap.ic_launcher;        text = "测试";        textColor = Color.BLACK;        hintTextColor = Color.GRAY;        // hintTextSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,HINT_TEXT_SIZE,getResources().getDisplayMetrics());        mPaint = new Paint();    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        //mPaint.setStrokeWidth(10);       // mPaint.setColor(Color.RED);        //canvas.drawLine(getWidth() - 20, 0, getWidth(), 20, mPaint);    }    public void setTextSize(int textSize) {        mTextView.setTextSize(textSize);    }    public void setTextSize(int unit, int textSize) {        mTextView.setTextSize(unit, textSize);    }    public void setText(String text) {        mTextView.setText(text);    }    public void setHintText(String hintText) {        mHintTextView.setText(hintText);    }    public void setHintTextSize(int hintTextSize) {        mHintTextView.setTextSize(hintTextSize);    }    public void setHintTextSize(int unit, int hintTextSize) {        mHintTextView.setTextSize(unit, hintTextSize);    }    public void setImageViewBitmap(Bitmap bt) {        mImageView.setImageBitmap(bt);    }    public void setImageView(int btId) {        mImageView.setImageResource(btId);    }    public TextView getTextView() {        return mTextView;    }    public TextView getHintTextView() {        return mHintTextView;    }    public ImageView getImageView() {        return mImageView;    }}

希望能对您有帮助

0 0