android linearlayout 自定义,android-我的自定义视图的默认属性值(从LinearLayout继承)...

我有一个自定义布局

public class PersonView extends LinearLayout{

public PersonView(Context context, AttributeSet attrs) {

super(context, attrs);

initUi(context);

}

public PersonView(Context context) {

super(context);

initUi(context);

}

private void initUi(Context context){

LayoutInflater.from(context).inflate(R.layout.person_view, this, true);

profilePicture = (ImageView)findViewById(R.id.profile_picture);

...

}

布局在xml中定义

...

然后我在其他布局中使用它

情况1

// In this case android:layout_margin is specified so it should be used

情况二

// In this case android:layout_margin is NOT specified so I want for my PersonView some default value should be used (say 5pt)

我的自定义布局PersonView如何实现案例2?

解决方法:

将android:layout_margin添加到attrs.xml

...

...

像其他自定义属性一样访问属性:

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PersonView);

float margin = a.getDimension(R.styleable.PersonView_android_layout_margin, 0);

...

boolean hasMargin = a.hasValue(R.styleable.PersonView_android_layout_margin);

标签:custom-controls,attributes,android

来源: https://codeday.me/bug/20191102/1994769.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值