我使用FragmentTransaction将一个Fragment添加到FrameLayout中。我想动态更改Fragment使用的RelativeLayout的边距。但是,使用RelativeLayout.layoutParams时,边距不会改变。我也使用了setMargins()
,但它没有用。
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.infocard, container, false);
RelativeLayout infoLayout = (RelativeLayout) view.findViewById(R.id.info);
infoLayout.setOnClickListener(new EmptyClickListener());
final int width = 250;
final int height = 320;
int leftMargin = 0;
int topMargin = 0;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
if (x - width < 0) {
leftMargin = 0;
System.out.println("left " + leftMargin);
}
else {
leftMargin = x - width;
}
if (y >= 450 && y <= 480) {
topMargin = y - height;
}
params.leftMargin = leftMargin;
params.topMargin = topMargin;
infoLayout.setLayoutParams(params);
答案 0 :(得分:1)
尝试使用
FrameLayout.LayoutParams
而不是
RelativeLayout.LayoutParams
布局期望params类型是其父容器中的类型,而不是您在其上设置的View类型