Android自定义布局包装内容
我正在实现自定义布局。 我将ImageView放到布局中,并在onMeasure方法中调用all children.measurew,h 一切正常,而不是包装内容行为。 视图的行为类似于匹配父视图,是全屏大小,而不是包装内容 当我想让视图成为包装内容时,请执行此操作Android自定义布局包装内容,android,Android,我正在实现自定义布局。 我将ImageView放到布局中,并在onMeasure方法中调用all children.measurew,h 一切正常,而不是包装内容行为。 视图的行为类似于匹配父视图,是全屏大小,而不是包装内容 当我想让视图成为包装内容时,请执行此操作 if (boxSize == MyBox.RATIO_MEASURE) { spec = MeasureSpec.AT_MOST; size = containerSi
if (boxSize == MyBox.RATIO_MEASURE) {
spec = MeasureSpec.AT_MOST;
size = containerSize;
代码:
人力资源管理。。。。这看起来很棘手,因为您是从Java内部完成的 我想你想要的魔法课是LayoutParams
我不关心视图的布局参数,我想根据我的自定义lobic box类来测量它。布局参数用于确定视图的大小。实际上,视图已经在xml中设置了wrap_内容。LayoutParams用于布局。BoxLayout是布局,它不依赖于LayoutParams
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
for (int i = 0; i < getChildCount(); i++) {
View v = getChildAt(i);
// custom layout logic
MyBox box = getBox(v);
int width = getMeasuredSpec(box.getWidth(), widthMeasureSpec);
int height = getMeasuredSpec(box.getHeight(), heightMeasureSpec);
v.measure(width, height);
}
}
private int getMeasuredSpec(float boxSize, int containerSpec) {
int containerSize = MeasureSpec.getSize(containerSpec);
int spec;
int size;
if (boxSize == MyBox.RATIO_MEASURE) {
spec = MeasureSpec.AT_MOST;
size = containerSize;
} else if (boxSize == 1) {
spec = MeasureSpec.AT_MOST;
size = containerSize;
} else {
spec = MeasureSpec.EXACTLY;
size = (int) (MyBox.isScreenDependent(boxSize)
? containerSize * boxSize
: boxSize);
}
return MeasureSpec.makeMeasureSpec(size, spec);
}
int WRAP_CONTENT= -2;
LayoutParams params= new ViewGroup.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
ImageView bNext_Word = (ImageView) rootView.findViewById(R.id.myimage);
mImage.setLayoutParams( params);