Android设置textview高度,Andriod自定义View一:TextView可设置宽高

第一步:建立一个MyCustomView继承View,实现构造方法

/**

* Created by chenshouyin on 17/3/9.

*/

public class MyCustomView extends View {

//1.此处应该继承View

//2.此处用this依次调用第二个第三个构造方法

public MyCustomView(Context context) {

//super(context);

this(context, null);

}

public MyCustomView(Context context, @Nullable AttributeSet attrs) {

//super(context, attrs);

this(context, attrs, 0);

}

public MyCustomView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

//3.此处得到attrs.xml里面,我们的自定义的样式属性 Attribute属性的意思,AttributeSet那么就是xml中的属性设置的意思

}

}

第二步:在res资源文件夹新建atts.xml文件设置自定义View需要用到的属性

`

**第三步:布局文件中引用自定义属性并且使用自定义属性**

2eecf72c54d6

Paste_Image.png

小技巧:主要输入custom:编译器会自动给我们导入

2eecf72c54d6

zhu

网上导入是这样子的

xmlns:custom="http://schemas.android.com/apk/res/hxing.com.mycustomtextview.MyCustomTextView

可能AS版本的问题,我这里会提示用

xmlns:custom="http://schemas.android.com/apk/res-auto"

2eecf72c54d6

csy.jpg

好了,到此,我们在布局文件也已经引入了自定义View,那么运行程序,是不是可以看到TextView了呢?别急,我们还没把View画出来呢。

第四步:设置画笔大小,颜色,字体。

private String mTextViewString;

private int mTextViewColor;

private int mTextViewSize;

思考:为什么MyCustomView中已经设置了文字,文字颜色,文字大小,还要设置画笔大小,颜色,字体呢?

custom:titleColor="#d41e1e"

custom:titleSize="16sp"

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值