- dimension: 引用字体大小
//定义
//使用:
app:text_size = “28sp”
或者
app:text_size = “@android:dimen/app_icon_size”
- enum:枚举值
//定义
//使用:
app:orientation = “vertical”
- flags:标志 (位或运行) 主要作用=可以多个值
//定义
// 使用
app:gravity = Top|left
- fraction:百分数:
//定义:
//使用:
app:transparency = “80%”
- reference:参考/引用某一资源ID
//定义:
//使用:
app:leftIcon = “@drawable/图片ID”
- 混合类型:属性定义时指定多种类型值
//属性定义
//使用
android:background = “@drawable/图片ID”
//或者
android:background = “#FFFFFF”
三、自定义控件类型
1. 自定义属性
在res/values
目录下的attrs.xml
文件中
2. 布局中使用自定义属性
在布局中使用
<com.myapplication.view.CustomView
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
app:leftIcon=“@mipmap/ic_temp”
app:name=“温度”
app:state=“false” />
3. view的构造函数获取自定义属性
class DigitalCustomView : LinearLayout {
constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
LayoutInflater.from(context).inflate(R.layout.view_custom, this)
var ta = context.obtainStyledAttributes(attrs, R.styleable.CustomView)
mIcon = ta.getResourceId(R.styleable.CustomView_leftIcon, -1) //左图像
mState = ta.getBoolean(R.styleable.DigitalCustomView_state, false)
mName = ta.getString(R.styleable.CustomView_name)
ta.recycle()
initView()
}
}
上面给出大致的代码 记得获取context.obtainStyledAttributes(attrs, R.styleable.CustomView)
最后要调用ta.recycle()
利用对象池回收ta加以复用
就是继承系统已经提供好给我们的控件例如TextView、LinearLayout等,分为View类型或者ViewGroup类型的两种。主要根据业务需求进行实现,实现重写的空间也很大 主要看需求。
比如需求 :在文字后面加个颜色背景
根据需要一般这种情况下我们是希望可以复用系统的onMeaseur
和onLayout
流程.直接复写onDraw
方法
class Practice02BeforeOnDrawView : AppCompatTextView {
internal var paint = Paint(Paint.ANTI_ALIAS_FLAG)
internal var b