阿里面试官: 自定义View跟绘制流程相关知识点?,2024年Android笔试题总

  • 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类型的两种。主要根据业务需求进行实现,实现重写的空间也很大 主要看需求。

比如需求 :在文字后面加个颜色背景

根据需要一般这种情况下我们是希望可以复用系统的onMeaseuronLayout流程.直接复写onDraw方法

class Practice02BeforeOnDrawView : AppCompatTextView {

internal var paint = Paint(Paint.ANTI_ALIAS_FLAG)

internal var b

  • 28
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值