博客
关于我
Android自定义带文字标题和方框的控件
阅读量:351 次
发布时间:2019-03-04

本文共 5753 字,大约阅读时间需要 19 分钟。

1.使LinearLayout布局带有方框线;

2.使LinearLayout布局Top方框线加入一个文字标题。

要想实现LinearLayout布局带有方框线,这个很简单,直接加一个方框线的布局即可。

但是要实现Top方框线加入一个文字标题,则需要自定义View来实现。

效果如下:

在这里插入图片描述
TitleBorderLayout.java

package com.hsae.audiodemos.view;import android.content.Context;import android.content.res.Resources;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.text.Layout;import android.text.TextPaint;import android.util.AttributeSet;import android.widget.LinearLayout;import com.hsae.audiodemos.R;public class TitleBorderLayout extends LinearLayout {   	private static float DEFAULT_TITLE_POSITION_SCALE = 0.48f;	public static int DEFAULT_BORDER_SIZE = 1;	public static int DEFAULT_BORDER_COLOR = Color.BLACK;	public static int DEAFULT_TITLE_COLOR = Color.BLACK;	private int mBorderPaneHeight ;		private Paint mBorderPaint;	private float mBorderSize;		private TextPaint mTextPaint;	private CharSequence mTitle;	private int mTitlePosition;		public TitleBorderLayout(Context context) {   		this(context, null);	}		/**	 * Construct a new TitleBorderLayout with default style, overriding specific style     * attributes as requested.	 * @param context	 * @param attrs	 */	public TitleBorderLayout(Context context, AttributeSet attrs) {   		super(context, attrs);		setWillNotDraw(false);				mBorderPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 		mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);		Resources res = getResources();		mTextPaint.density = res.getDisplayMetrics().density;				TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TitleBorderLayout);				mTitle = a.getText(R.styleable.TitleBorderLayout_title);		int titleColor = a.getColor(R.styleable.TitleBorderLayout_titleTextColor, DEAFULT_TITLE_COLOR);		mTextPaint.setColor(titleColor);				float titleTextSize = a.getDimension(R.styleable.TitleBorderLayout_titleTextSize, 0);		if (titleTextSize > 0) {   			mTextPaint.setTextSize(titleTextSize);		}				mTitlePosition = a.getDimensionPixelSize(R.styleable.TitleBorderLayout_titlePosition, -1); 		mBorderSize = a.getDimensionPixelSize(R.styleable.TitleBorderLayout_borderSize, DEFAULT_BORDER_SIZE);				int borderColor = a.getColor(R.styleable.TitleBorderLayout_borderColor, DEFAULT_BORDER_COLOR);		mBorderPaint.setColor(borderColor);				a.recycle();	}		/**	 * Get the color of border.	 * @return	 */	public int getBorderColor() {   		return mBorderPaint.getColor();	} 	/**	 * Set the color of border.	 * @param borderColor	 */	public void setBorderColor(int borderColor) {   		mBorderPaint.setColor(borderColor);		requestLayout();	} 	/**	 * Get the size of border.	 * @return	 */	public float getBorderSize() {   		return mBorderSize;	} 	/**	 * Set the size of border.	 * @param borderSize	 */	public void setBorderSize(float borderSize) {   		mBorderSize = borderSize;		requestLayout();	} 	/**	 * Get the color of title.	 * @return	 */	public int getTitleColor() {   		return mTextPaint.getColor();	} 	/**	 * Set the color of title.	 * @param titleColor	 */	public void setTitleColor(int titleColor) {   		mTextPaint.setColor(titleColor);		requestLayout();	} 	/**	 * Get the size of title.	 * @return	 */	public float getTitleTextSize() {   		return mTextPaint.getTextSize();	} 	/**	 * Set the size of title.	 * @param titleTextSize	 */	public void setTitleTextSize(float titleTextSize) {   		mTextPaint.setTextSize(titleTextSize);		requestLayout();	}		/**	 * Get the title.	 * @return	 */	public CharSequence getTitle() {   		return mTitle;	} 	/**	 * Set the title which will be shown on the top of border pane. 	 * @param title	 */	public void setTitle(CharSequence title) {   		mTitle = title;		requestLayout();	}		/**	 * Get the position of title.	 * @return	 */	public int getTitlePosition() {   		return mTitlePosition;	} 	/**	 * Set the position of title where the paint will start to draw.	 * @param titlePosition	 */	public void setTitlePosition(int titlePosition) {   		mTitlePosition = titlePosition;		requestLayout();	} 	/**	 * Get the height of border pane, it's different from the layout height!	 * @return	 */	public int getBorderPaneHeight() {   		return mBorderPaneHeight;	} 	/**	 * Draw the title border	 * @param canvas 	 */	@Override	protected void onDraw(Canvas canvas) {   		super.onDraw(canvas);				Paint.FontMetrics fm = mTextPaint.getFontMetrics();		final float titleHeight =  fm.descent - fm.ascent; 		final CharSequence titleText = (mTitle == null) ? "" : mTitle;		final float titleWidth = Layout.getDesiredWidth(titleText, mTextPaint);				final int width = getWidth();		final int height = getHeight();				if (mTitlePosition <= 0 || mTitlePosition + titleWidth > width) {   			mTitlePosition = (int) (DEFAULT_TITLE_POSITION_SCALE * width); 		}				final float topBorderStartY = titleHeight / 3f - mBorderSize / 2;				mBorderPaneHeight = (int) Math.ceil(height - topBorderStartY);	    /* draw border*/		// top		canvas.drawRect(0, topBorderStartY, mTitlePosition, topBorderStartY + mBorderSize, mBorderPaint);		canvas.drawText(titleText.toString(), mTitlePosition, titleHeight / 3 * 2f, mTextPaint); //title		canvas.drawRect(mTitlePosition + titleWidth, topBorderStartY, width, topBorderStartY + mBorderSize, mBorderPaint);        // left        canvas.drawRect(0, topBorderStartY, mBorderSize, height, mBorderPaint);        // right        canvas.drawRect(width - mBorderSize, topBorderStartY, width, height, mBorderPaint);        // down        canvas.drawRect(0, height - mBorderSize, width, height, mBorderPaint);	}}

attrs.xml

属性使用方法:

app:title="布局1"app:titleTextSize="16dp"

转载地址:http://edre.baihongyu.com/

你可能感兴趣的文章
Webpack 之 basic chunk graph
查看>>
Mysql5.7版本单机版my.cnf配置文件
查看>>
mysql5.7的安装和Navicat的安装
查看>>
mysql5.7示例数据库_Linux MySQL5.7多实例数据库配置
查看>>
Mysql8 数据库安装及主从配置 | Spring Cloud 2
查看>>
mysql8 配置文件配置group 问题 sql语句group不能使用报错解决 mysql8.X版本的my.cnf配置文件 my.cnf文件 能够使用的my.cnf配置文件
查看>>
MySQL8.0.29启动报错Different lower_case_table_names settings for server (‘0‘) and data dictionary (‘1‘)
查看>>
MYSQL8.0以上忘记root密码
查看>>
Mysql8.0以上重置初始密码的方法
查看>>
mysql8.0新特性-自增变量的持久化
查看>>
Mysql8.0注意url变更写法
查看>>
Mysql8.0的特性
查看>>
MySQL8修改密码报错ERROR 1819 (HY000): Your password does not satisfy the current policy requirements
查看>>
MySQL8修改密码的方法
查看>>
Mysql8在Centos上安装后忘记root密码如何重新设置
查看>>
Mysql8在Windows上离线安装时忘记root密码
查看>>
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>