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

8次阅读

共计 555 个字符,预计需要花费 2 分钟才能阅读完成。

有时候要根据自己的需要定制自己的组件的位置,可以直接继承 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);

正文完
 0