`

android

阅读更多
在/values/目录下新建attr.xml文件,内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="LabelView">
        <attr format="string" name="text" />
        <attr format="color" name="textColor" />
        <attr format="dimension" name="textSize" />
    </declare-styleable>

</resources>
attr.xml文件就是我们自定义的属性文件。
在main.xml布局文件中使用自定义的属性:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:labelview="http://schemas.android.com/apk/res/com.study.app.android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <com.study.app.android.LabelView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        labelview:text="自定义labelview控件"
        labelview:textColor="#ffcecece"
        labelview:textSize="20sp" >
    </com.study.app.android.LabelView>

</LinearLayout>
注意:首先将自定义属性引用进来xmlns:labelview="http://schemas.android.com/apk/res/com.study.app.android",其中labelview任意命名,com.study.app.android是应用的包名。
这样在代码中就可以使用自定义属性了
package com.study.app.android;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

public class LabelView extends View {
private Paint mTextPaint;
private String mText;
private int mAscent;

public LabelView(Context context) {
super(context);
initLabelView();
}

public LabelView(Context context, AttributeSet attrs) {
super(context, attrs);
initLabelView();

TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.LabelView);

CharSequence s = a.getString(R.styleable.LabelView_text);
if (s != null) {
setText(s.toString());
}
setTextColor(a.getColor(R.styleable.LabelView_textColor, 0xFF000000));

int textSize = a.getDimensionPixelOffset(
R.styleable.LabelView_textSize, 0);
if (textSize > 0) {
setTextSize(textSize);
}

a.recycle();
}

private final void initLabelView() {
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density);
mTextPaint.setColor(0xFF000000);
setPadding(3, 3, 3, 3);
}

public void setText(String text) {
mText = text;
requestLayout();
invalidate();
}

public void setTextSize(int size) {
mTextPaint.setTextSize(size);
requestLayout();
invalidate();
}
public void setTextColor(int color) {
mTextPaint.setColor(color);
invalidate();
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(measureWidth(widthMeasureSpec),
measureHeight(heightMeasureSpec));
}

private int measureWidth(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) mTextPaint.measureText(mText) + getPaddingLeft()
+ getPaddingRight();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}

return result;
}

private int measureHeight(int measureSpec) {
int result = 0;
int specMode = MeasureSpec.getMode(measureSpec);
int specSize = MeasureSpec.getSize(measureSpec);

mAscent = (int) mTextPaint.ascent();
if (specMode == MeasureSpec.EXACTLY) {
result = specSize;
} else {
result = (int) (-mAscent + mTextPaint.descent()) + getPaddingTop()
+ getPaddingBottom();
if (specMode == MeasureSpec.AT_MOST) {
result = Math.min(result, specSize);
}
}
return result;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent,
mTextPaint);
}
}
参考文章:
declare-styleable:自定义控件的属性
http://blog.csdn.net/congqingbin/article/details/7869730

Android 深入理解Android中的自定义属性
http://blog.csdn.net/lmj623565791/article/details/45022631

Android资源文件res的使用详解(strings,layout,drawable,arrays等)
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0204/838.html
分享到:
评论

相关推荐

    android 经典代码例子

    在Android开发领域,经典代码例子是开发者学习和提升技能的重要资源。这些例子涵盖了各种关键功能和组件的实现,有助于深入理解Android应用的工作原理。在这个压缩包中,我们可能找到了多个有关Android编程的示例...

    Android开发入门60个小案例+源代码

    在Android开发领域,初学者经常会面临许多挑战,如理解Android应用程序的基本架构、学习XML布局、掌握Java或Kotlin编程语言,以及如何与设备硬件交互等。"Android开发入门60个小案例+源代码"这个资源提供了丰富的...

    Android省市区三级联动滚轮选择——Cascade_Master

    该组件是基于开源库`Android-wheel`实现的,`Android-wheel`是一个适用于Android的滚轮选择器,它可以创建类似于iOS中PickerView的效果,让用户通过滚动来选取所需的数据。在省市区三级联动中,当用户在一级(省)...

    Android自定义日期选择器源码

    在Android开发中,系统默认的日期和时间选择器虽然实用,但往往无法满足所有场景的需求。因此,开发者经常需要自定义日期选择器来提供更符合应用风格或特定功能的交互体验。这篇内容将深入探讨如何在Android中创建一...

    Android通过webservice连接Sqlserver实例

    在Android开发中,有时我们需要与远程数据库进行交互,例如SQLServer。这个场景通常是通过Web服务,如WebService来实现。本文将详细介绍如何在Android应用中利用WebService接口连接到SQLServer数据库,实现数据的增...

    Android串口通信(Android Studio)

    在Android开发中,串口通信(Serial Port Communication)是一种重要的技术,它允许设备之间通过串行接口进行数据交换。在Android Studio环境下实现串口通信,开发者可以构建与硬件设备交互的应用,例如读取传感器...

    Android组件设计思想

    Android应用开发的哲学是把一切都看作是组件。把应用程序组件化的好处是降低模块间的耦合性,同时提高模块的复用性。Android的组件设计思想与传统的组件设计思想最大的区别在于,前者不依赖于进程。也就是说,进程...

    新版Android开发教程.rar

    ----------------------------------- Android 编程基础 1 封面----------------------------------- Android 编程基础 2 开放手机联盟 --Open --Open --Open --Open Handset Handset Handset Handset Alliance ...

    Android 完美实现图片圆角和圆形

    在Android开发中,有时我们需要对显示的图片进行特殊处理,比如让图片呈现圆角或完全圆形。本知识点将深入探讨如何在Android应用中完美实现图片的圆角和圆形效果。 首先,我们来看如何实现图片的圆角效果。Android...

    Android.bp文件说明.pdf

    Android新编译规则Android.bp文件语法规则详细介绍,条件编译的配置案例。 Android.bp 文件首先是 Android 系统的一种编译配置文件,是用来代替原来的 Android.mk 文件的。在 Android7.0 以前,Android 都是使用 ...

    JS调用Android方法,向Android方法传递json数据

    在现代的移动应用开发中,JavaScript与原生平台之间的交互变得越来越常见,特别是在使用Android的WebView组件时。本文将深入探讨如何使用JavaScript调用Android的方法,并传递JSON数据,以实现两者之间的高效通信。 ...

    Android扫雷游戏(基于Android Studio)

    【Android扫雷游戏开发详解】 在移动开发领域,Android Studio是Google推出的官方集成开发环境(IDE),用于构建Android应用程序。本项目"Android扫雷游戏"就是利用Android Studio进行开发的一个实例,旨在帮助初学...

    android应用开发范例精解

    第2篇为应用开发篇,通过实例介绍了Android UI布局、Android人机界面、手机硬件设备的使用、Android本地存储系统、Android中的数据库、多线程设计、Android传感器、Android游戏开发基础、Android与Internet,以及...

    android 仿微信语音聊天demo

    【Android 微信语音聊天Demo】是一个典型的移动应用开发示例,主要展示了如何在Android平台上构建类似微信的语音聊天功能。这个Demo包含了按钮状态切换、语音录制、本地存储、回放和加载等一系列关键操作,是Android...

    Android SDK离线包合集(Android 4.0-5.0)

    Android SDK离线包合集(Android 4.0-5.0)。不用去Google下载,直接国内下载离线包,各版本文件独立,任意下载。手机流量上传了一部分,好心疼。如不能下载,请告诉我更新地址。 附上简单教程。 这是Android开发所...

    Android小项目集合100多个

    1. **Android SDK**:Android软件开发工具包(SDK)是开发Android应用的基础,包含了开发、调试和发布应用所需的所有工具,如Android Studio IDE、Java Development Kit(JDK)、模拟器以及各种版本的Android平台库...

    Android基于Socket聊天最终版

    在本项目中,“Android基于Socket聊天最终版”是一个实现了基于Socket通信的简易聊天应用,它模仿了QQ的一些功能。这个项目包含三个主要部分:服务器端、客户端和数据库管理。通过Socket编程,Android设备可以作为...

    Android 图片浏览全屏缩放

    在Android开发中,实现图片浏览的全屏缩放效果是一项常见的需求,特别是在社交应用中,如QQ好友动态和微信朋友圈。这种功能不仅需要提供良好的用户体验,还需要考虑性能和内存优化,因为图片通常较大,处理不当可能...

Global site tag (gtag.js) - Google Analytics