Android高手进阶教程(四)之----Android 中自定义属性(attr.xml,TypedArray)的使用!

本文介绍如何在Android中自定义XML属性。通过创建attrs.xml文件并定义样式,可以在自定义视图中使用这些属性。文章提供了详细的步骤和代码示例。

今天我们的教程是根据前面一节扩展进行的,如果你没有看,请点击 Android高手进阶教程(三) 查看第三课,这样跟容易方便你的理解!

在xml 文件里定义控件的属性,我们已经习惯了android:attrs="" ,那么我们能不能定义自己的属性能,比如:test:attrs="" 呢?答案是肯定的.

好了我就不卖关子了,直接进入主题。大致以下步骤:


一、在res/values文件下定义一个attrs.xml文件.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
    </declare-styleable>
</resources>

二、 我们在MyView.java 代码修改如下,其中下面的构造方法是重点,我们获取定义的属性我们R.sytleable.MyView_textColor, 获取方法中后面通常设定默认值(float textSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我们在xml 文件中没有定义.从而使用默认值!

获取,MyView 就是定义在<declare-styleable name="MyView "></declare-styleable> 里的名字,获取里面属性用 名字_ 属性 连接起来就可以.TypedArray 通常最后调用 .recycle() 方法,为了保持以后使用该属性一致性!

view plaincopy to clipboardprint?
public MyView(Context context,AttributeSet attrs)  
    {  
        super(context,attrs);  
        mPaint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.MyView);  
          
        int textColor = a.getColor(R.styleable.MyView_textColor,  
                0XFFFFFFFF);  
        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
          
        mPaint.setTextSize(textSize);  
        mPaint.setColor(textColor);  
          
        a.recycle();  
    } 
public MyView(Context context,AttributeSet attrs)
 {
  super(context,attrs);
  mPaint = new Paint();
  
  TypedArray a = context.obtainStyledAttributes(attrs,
    R.styleable.MyView);
  
  int textColor = a.getColor(R.styleable.MyView_textColor,
    0XFFFFFFFF);
  float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  
  mPaint.setTextSize(textSize);
  mPaint.setColor(textColor);
  
  a.recycle();
 }

MyView.java 全部代码如下:

view plaincopy to clipboardprint?
package com.android.tutor;  
import android.content.Context;  
import android.content.res.TypedArray;  
import android.graphics.Canvas;  
import android.graphics.Color;  
import android.graphics.Paint;  
import android.graphics.Rect;  
import android.graphics.Paint.Style;  
import android.util.AttributeSet;  
import android.view.View;  
public class MyView extends View {  
    private Paint mPaint;  
    private Context mContext;  
    private static final String mString = "Welcome to Mr Wei's blog";  
      
    public MyView(Context context) {  
        super(context);  
        mPaint = new Paint();  
    }  
    public MyView(Context context,AttributeSet attrs)  
    {  
        super(context,attrs);  
        mPaint = new Paint();  
          
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.MyView);  
          
        int textColor = a.getColor(R.styleable.MyView_textColor,  
                0XFFFFFFFF);  
        float textSize = a.getDimension(R.styleable.MyView_textSize, 36);  
          
        mPaint.setTextSize(textSize);  
        mPaint.setColor(textColor);  
          
        a.recycle();  
    }  
    @Override 
    protected void onDraw(Canvas canvas) {  
        // TODO Auto-generated method stub  
        super.onDraw(canvas);  
        //设置填充  
        mPaint.setStyle(Style.FILL);  
          
        //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标  
        canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);  
          
        mPaint.setColor(Color.BLUE);  
        //绘制文字  
        canvas.drawText(mString, 10, 110, mPaint);  
    }  

package com.android.tutor;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
 private Paint mPaint;
 private Context mContext;
 private static final String mString = "Welcome to Mr Wei's blog";
 
 public MyView(Context context) {
  super(context);
  mPaint = new Paint();
 }
 public MyView(Context context,AttributeSet attrs)
 {
  super(context,attrs);
  mPaint = new Paint();
  
  TypedArray a = context.obtainStyledAttributes(attrs,
    R.styleable.MyView);
  
  int textColor = a.getColor(R.styleable.MyView_textColor,
    0XFFFFFFFF);
  float textSize = a.getDimension(R.styleable.MyView_textSize, 36);
  
  mPaint.setTextSize(textSize);
  mPaint.setColor(textColor);
  
  a.recycle();
 }
 @Override
 protected void onDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.onDraw(canvas);
  //设置填充
  mPaint.setStyle(Style.FILL);
  
  //画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标
  canvas.drawRect(new Rect(10, 10, 100, 100), mPaint);
  
  mPaint.setColor(Color.BLUE);
  //绘制文字
  canvas.drawText(mString, 10, 110, mPaint);
 }
}
 

三、将我们自定义的MyView 加入布局main.xml 文件中,平且使用自定义属性,自定义属性必须加上:

      xmlns:test ="http://schemas.android.com/apk/res/com.android.tutor "蓝色 是自定义属性的前缀,红色 是我们包名.

main.xml 全部代码如下:

view plaincopy to clipboardprint?
<?xml   
version="1.0" encoding="utf-8"?>  
<LinearLayout   
xmlns:android="
http://schemas.android.com/apk/res/android
                
xmlns:test="
http://schemas.android.com/apk/res/com.android.tutor
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    >  
<TextView    
    android:layout_width="fill_parent"   
    android:layout_height="wrap_content"   
    android:text="@string/hello" 
    />  
<com.android.tutor.MyView  
    android:layout_width="fill_parent"   
    android:layout_height="fill_parent"   
    test:textSize="20px" 
    test:textColor="#fff" 
/>  
</LinearLayout> 
<?xml
version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="
http://schemas.android.com/apk/res/android"
             
xmlns:test="
http://schemas.android.com/apk/res/com.android.tutor"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/hello"
    />
<com.android.tutor.MyView
 android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    test:textSize="20px"
    test:textColor="#fff"
/>
</LinearLayout>
 

四、运行之效果如下图:

 

今天就到此结束,大家有什么疑问的,请留言,我会及时答复大家!谢谢~

 

本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/Android_Tutor/archive/2010/04/20/5508615.aspx

本项目采用C++编程语言结合ROS框架构建了完整的双机械臂控制系统,实现了Gazebo仿真环境下的协同运动模拟,并完成了两台实体UR10工业机器人的联动控制。该毕业设计在答辩环节获得98分的优异成绩,所有程序代码均通过系统性调试验证,保证可直接部署运行。 系统架构包含三个核心模块:基于ROS通信架构的双臂协调控制器、Gazebo物理引擎下的动力学仿真环境、以及真实UR10机器人的硬件接口层。在仿真验证阶段,开发了双臂碰撞检测算法和轨迹规划模块,通过ROS控制包实现了末端执行器的同步轨迹跟踪。硬件集成方面,建立了基于TCP/IP协议的实时通信链路,解决了双机数据同步和运动指令分发等关键技术问题。 本资源适用于自动化、机械电子、人工智能等专业方向的课程实践,可作为高年级课程设计、毕业课题的重要参考案例。系统采用模块化设计理念,控制核心与硬件接口分离架构便于功能扩展,具备工程实践能力的学习者可在现有框架基础上进行二次开发,例如集成视觉感知模块或优化运动规划算法。 项目文档详细记录了环境配置流程、参数调试方法和实验验证数据,特别说明了双机协同作业时的时序同步解决方案。所有功能模块均提供完整的API接口说明,便于使用者快速理解系统架构并进行定制化修改。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
【微电网】【创新点】基于非支配排序的蜣螂优化算法NSDBO求解微电网多目标优化调度研究(Matlab代码实现)内容概要:本文围绕基于非支配排序的蜣螂优化算法(NSDBO)在微电网多目标优化调度中的应用展开研究,提出了一种改进的智能优化算法以解决微电网系统中经济性、环保性和能源效率等多重目标之间的权衡问题。通过引入非支配排序机制,NSDBO能够有效处理多目标优化中的帕累托前沿搜索,提升解的多样性和收敛性,并结合Matlab代码实现仿真验证,展示了该算法在微电网调度中的优越性能和实际可行性。研究涵盖了微电网典型结构建模、目标函数构建及约束条件处理,实现了对风、光、储能及传统机组的协同优化调度。; 适合人群:具备一定电力系统基础知识和Matlab编程能力的研究生、科研人员及从事微电网、智能优化算法应用的工程技术人员;熟悉优化算法与能源系统调度的高年级本科生亦可参考。; 使用场景及目标:①应用于微电网多目标优化调度问题的研究与仿真,如成本最小化、碳排放最低与供电可靠性最高之间的平衡;②为新型智能优化算法(如蜣螂优化算法及其改进版本)的设计与验证提供实践案例,推动其在能源系统中的推广应用;③服务于学术论文复现、课题研究或毕业设计中的算法对比与性能测试。; 阅读建议:建议读者结合文中提供的Matlab代码进行实践操作,重点关注NSDBO算法的核心实现步骤与微电网模型的构建逻辑,同时可对比其他多目标算法(如NSGA-II、MOPSO)以深入理解其优势与局限,进一步开展算法改进或应用场景拓展。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值