Android自定义带标题边框的Layout
生活随笔
收集整理的這篇文章主要介紹了
Android自定义带标题边框的Layout
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
? ? ?今天工作中又碰到個小問題,項目需要用到像Java Swing的JPanel一樣帶標題邊框的布局,Android里沒有類似控件,想到這個也不難,自己畫了一個,是繼承LinearLayout的一個自定義布局,當然,可以根據(jù)需要繼承其他布局,基本都是一樣的過程。
? ? ? 當然這個自定義布局有點瑕疵,就是標題占用了布局的一部分高度,子控件需要調(diào)整在布局中的垂直位置來避免和標題邊框靠得過緊。
------------------本博客如未明正聲明轉(zhuǎn)載,皆為原創(chuàng),轉(zhuǎn)載請注明出處!------------------
下面貼代碼:
/*** 一個像java swing的JPanel控件一樣可以帶標題邊框的布局,可以指定標題位置、顏色、字體大小。* 另外還可以控制邊框大小和邊框的顏色。但是,子控件指定垂直布局適應(yīng)的時候,子控件看起來并不在布局中間,* 因為標題高度占用了布局的一部分垂直空間,使用時子控件需要指定Margin Top,否則可能和標題重疊。* * This is a layout with title border, you can set a title just like Java-Swing JPanel.* and you can control the position, the color and the size of the title. In addition,* the border's color and size are always controlled.* * !FIXME: The title has its own height, so when the children's gravity set as {@link Gravity#CENTER} * or {@link Gravity#CENTER_VERTICAL} do not work well.* * @date 2013/09/24* @author Wison*/ public class TitleBorderLayout extends LinearLayout {/** 默認情況下標題在總長度的1/10處顯示 */private static float DEFAULT_TITLE_POSITION_SCALE = 0.1f;public static int DEFAULT_BORDER_SIZE = 1;public static int DEFAULT_BORDER_COLOR = Color.GRAY;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 */@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);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);/* 畫標題邊框 */// 上canvas.drawRect(0, topBorderStartY, mTitlePosition, topBorderStartY + mBorderSize, mBorderPaint);canvas.drawText(titleText.toString(), mTitlePosition, titleHeight / 3 * 2f, mTextPaint); // 標題canvas.drawRect(mTitlePosition + titleWidth, topBorderStartY, width, topBorderStartY + mBorderSize, mBorderPaint);// 左canvas.drawRect(0, topBorderStartY, mBorderSize, height, mBorderPaint);// 右canvas.drawRect(width - mBorderSize, topBorderStartY, width, height, mBorderPaint);// 下canvas.drawRect(0, height - mBorderSize, width, height, mBorderPaint);}}
下面是效果圖:
總結(jié)
以上是生活随笔為你收集整理的Android自定义带标题边框的Layout的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Kodak Preps 8 for Ma
- 下一篇: 【eoeAndroid特刊】第一期到第十