Android 在相对布局上设置边距不起作用
我使用FragmentTransaction将片段添加到FrameLayout中。我想动态更改片段使用的RelativeLayout的边距。但是,页边距不会随RelativeLayout.layoutParams而改变。我还使用了Android 在相对布局上设置边距不起作用,android,layout,android-relativelayout,margins,Android,Layout,Android Relativelayout,Margins,我使用FragmentTransaction将片段添加到FrameLayout中。我想动态更改片段使用的RelativeLayout的边距。但是,页边距不会随RelativeLayout.layoutParams而改变。我还使用了setMargins(),但它不起作用 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View v
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);
创建视图时的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图=充气机。充气(R.layout.infocard,container,false);
RelativeLayout infoLayout=(RelativeLayout)view.findViewById(R.id.info);
setOnClickListener(新的EmptyClickListener());
最终整数宽度=250;
最终内部高度=320;
int leftMargin=0;
int-topMargin=0;
RelativeLayout.LayoutParams params=新的RelativeLayout.LayoutParams(宽度、高度);
如果(x-宽度<0){
leftMargin=0;
System.out.println(“左”+左边距);
}
否则{
leftMargin=x-宽度;
}
如果(y>=450&&y尝试使用
FrameLayout.LayoutParams
而不是
RelativeLayout.LayoutParams
布局期望参数类型是其父容器中的类型,而不是您正在设置它们的视图类型出于某种原因,这在运行3.2.1的东芝标签上有效,但是,它在Kindle Fire 2.3.4上不起作用。它会引发任何错误吗?您在KF(您发布的方式)上尝试过其他方式吗看看它是否倒退?它没有抛出任何错误。我发布的方式在kindle fire上不起作用。奇怪。这可能与对KF的操作系统所做的一些更改有关。不过,我不太确定。我发布的方式是正确的,也是安卓所期望的。如果它不起作用,那么可能尝试清理您的项目,unin暂停当前设备上的一个,然后重新安装(只是为了用尽一切可能)。如果仍然发生这种情况,很可能是由于KF处理LayoutParams的特定方式。如果有人能够解决这个问题,我专门为Kindle Fire发了一篇新帖子。