string.format float Android自定义View详解

电脑杂谈  发布时间:2018-02-22 14:32:19  来源:网络整理

float与string_string.format float_string.format用法

很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义View上面花一些功夫,多写一些文章。先总结下自定义View的步骤:

1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

[ 3、重写onMesure ]

4、重写onDraw

我把3用[]标出了,所以说3不一定是必须的,当然了大部分情况下还是需要重写的。

1、自定义View的属性,首先在res/values/ 下建立一个attrs.xml , 在里面定义我们的属性和声明我们的整个样式。

<xml version="1.0" encoding="utf-8"> <resources><attr format="string" /> <attr format="color" /> <attr format="dimension" /><declare-styleable><attr /><attr /><attr /> </declare-styleable></resources>

我们定义了字体,字体颜色,字体大小3个属性,format是值该属性的取值类型:

一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。

float与string_string.format用法_string.format float

然后在布局中声明我们的自定义View

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01" android:layout_width="match_parent" android:layout_height="match_parent" ><com.example.customview01.view.CustomTitleViewandroid:layout_width="200dp"android:layout_height="100dp"custom:titleText="3712"custom:titleTextColor="#ff0000"custom:titleTextSize="40sp" /></RelativeLayout>

一定要引入 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"我们的命名空间,后面的包路径指的是项目的package

2、在View的构造方法中,获得我们的自定义的样式

/*** 文本*/ private String mTitleText; /*** 文本的颜色*/ private int mTitleTextColor; /*** 文本的大小*/ private int mTitleTextSize;/*** 绘制时控制文本绘制的范围*/ private Rect mBound; private Paint mPaint;public CustomTitleView(Context context, AttributeSet attrs) {this(context, attrs, 0); }public CustomTitleView(Context context) {this(context, null); }/*** 获得我自定义的样式属性** @param context* @param attrs* @param defStyle*/ public CustomTitleView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);/*** 获得我们所定义的自定义样式属性*/TypedArray a = context。


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-88868-1.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...