我的自定义视图的默认属性值(继承自 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 中定义的
<merge xmlns:android="http://schemas.android.com/apk/res/android">
...
然后我在其他布局中使用它
案例 1
// In this case android:layout_margin is specified so it should be used
<my.package.PersonView android:layout_margin="10dp" .../>
案例 2
// In this case android:layout_margin is NOT specified so I want for my PersonView some default value should be used (say 5pt)
<my.package.PersonView .../>
我应该如何让我的自定义布局 PersonView 实现案例 2?
I have a custom layout
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);
...
}
Layout is defined in xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
...
Then I use it in other layout
Case 1
// In this case android:layout_margin is specified so it should be used
<my.package.PersonView android:layout_margin="10dp" .../>
Case 2
// In this case android:layout_margin is NOT specified so I want for my PersonView some default value should be used (say 5pt)
<my.package.PersonView .../>
What should I do for my custom layout PersonView to achieve Case 2?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似的问题已在 https://stackoverflow.com/a/25982512/3554436
添加
android:layout_margin 解决
到attrs.xml
像其他自定义属性一样访问该属性:
Similar problem has been solved at https://stackoverflow.com/a/25982512/3554436
Add
android:layout_margin
toattrs.xml
Access the attribute like other custom attributes: