有没有办法以编程方式告诉哪种类型的布局是视图的父级
Is there a way to tell programmatically which type of layout is a view's parent
我有一个正在动态调整大小的 ImageView
,我需要知道 ImageView
的父级是 RelativeLayout
还是 LinearLayout
。有没有办法以编程方式告诉这个?
public class ResizeableImage extends ImageView {
View parent = null;
public ResizeableImage(Context context, AttributeSet attrs, int defaultStyle) {
super(context, attrs, defaultStyle);
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
parent = (View) this.getParent();
int parentHeight = parent.getHeight();
int parentWidth = parent.getWidth();
//parent.setMinimumHeight(yNew);
//parent.setMinimumWidth(xNew);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) parent.getLayoutParams();
params.height = yNew;
params.width = xNew;
parent.setLayoutParams(params);
}
}
为了设置父级的新尺寸(以及可扩展性),我需要知道它是 RelativeLayout 还是任何其他类型。
调用 getParent()
并进行 instanceof
检查以查看它是否是某种类型的兴趣(例如,getParent() instanceof RelativeLayout
)。
请注意,在这种情况下,width
和 height
是在 ViewGroup.LayoutParams
上定义的,所有 LayoutParams
class 的基础 class es,所以你可以转向那个,避免把自己束缚在 RelativeLayout
等
我有一个正在动态调整大小的 ImageView
,我需要知道 ImageView
的父级是 RelativeLayout
还是 LinearLayout
。有没有办法以编程方式告诉这个?
public class ResizeableImage extends ImageView {
View parent = null;
public ResizeableImage(Context context, AttributeSet attrs, int defaultStyle) {
super(context, attrs, defaultStyle);
}
@Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld)
{
super.onSizeChanged(xNew, yNew, xOld, yOld);
parent = (View) this.getParent();
int parentHeight = parent.getHeight();
int parentWidth = parent.getWidth();
//parent.setMinimumHeight(yNew);
//parent.setMinimumWidth(xNew);
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) parent.getLayoutParams();
params.height = yNew;
params.width = xNew;
parent.setLayoutParams(params);
}
}
为了设置父级的新尺寸(以及可扩展性),我需要知道它是 RelativeLayout 还是任何其他类型。
调用 getParent()
并进行 instanceof
检查以查看它是否是某种类型的兴趣(例如,getParent() instanceof RelativeLayout
)。
请注意,在这种情况下,width
和 height
是在 ViewGroup.LayoutParams
上定义的,所有 LayoutParams
class 的基础 class es,所以你可以转向那个,避免把自己束缚在 RelativeLayout
等