博客
关于我
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/

你可能感兴趣的文章
multi_index_container
查看>>
MySQL DBA 进阶知识详解
查看>>
Mura CMS processAsyncObject SQL注入漏洞复现(CVE-2024-32640)
查看>>
Mysql DBA 高级运维学习之路-DQL语句之select知识讲解
查看>>
mysql deadlock found when trying to get lock暴力解决
查看>>
MuseTalk如何生成高质量视频(使用技巧)
查看>>
mutiplemap 总结
查看>>
MySQL DELETE 表别名问题
查看>>
MySQL Error Handling in Stored Procedures---转载
查看>>
MVC 区域功能
查看>>
MySQL FEDERATED 提示
查看>>
mysql generic安装_MySQL 5.6 Generic Binary安装与配置_MySQL
查看>>
Mysql group by
查看>>
MySQL I 有福啦,窗口函数大大提高了取数的效率!
查看>>
mysql id自动增长 初始值 Mysql重置auto_increment初始值
查看>>
MySQL in 太多过慢的 3 种解决方案
查看>>
MySQL InnoDB 三大文件日志,看完秒懂
查看>>
Mysql InnoDB 数据更新导致锁表
查看>>
Mysql Innodb 锁机制
查看>>
MySQL InnoDB中意向锁的作用及原理探
查看>>