【android】自定义布局控制控件的位置可以通过继承FrameLayout实现

有时候要根据自己的需要定制自己的组件的位置,可以直接继承ViewGroup来实现,但是这种方法相对比较麻烦,要自己造比较多的轮子。可以通过继承FrameLayout来实现。
在FrameLayout的子类中控制控件,可以通过子控件的margin来设置位置:
HashMap.Entry entry = (HashMap.Entry) iter.next();
TRItemNode item = (TRItemNode) entry.getValue();

int id = item.getId();
TextView textView = mTextViewMap.get(id);

int width = textView.getMeasuredWidth();
int height = textView.getMeasuredHeight();

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
params.gravity = Gravity.LEFT;
params.leftMargin = item.left + width / 2;
params.topMargin = item.top + height / 2;
textView.setLayoutParams(params);

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理