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" .../>
情况二
// 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?
解决方法:
类似问题已在https://stackoverflow.com/a/25982512/3554436解决
将android:layout_margin添加到attrs.xml
<declare-styleable name="PersonView">
...
<attr name="android:layout_margin" />
...
</declare-styleable>
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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。