2012-03-21 70 views
1

这与我的previous question类似,但它不适用于Kindle Fire(2.3.4)。
我使用FragmentTransaction将一个片段添加到FrameLayout中。我想动态地改变片段使用的RelativeLayout的边距。在RelativeLayout上设置页边距在Kindle Fire上不起作用

但是,Kindle Fire上的FrameLayout.layoutParams的边距没有变化。但是,这适用于3.2.1。我也尝试使用setMargins(),它没有工作。

有谁知道我如何动态地改变Kindle Fire的边距?

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 = 270; 
     int leftMargin = 0; 
     int topMargin = 0; 
     FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height); 

     if (x - width < 0) { 
      leftMargin = 0; 
     } else { 
      leftMargin = x - width + 40; 
     } 

     if (y >= 400 && y <= 480) { 
      topMargin = 160; 
     } 

     params.leftMargin = leftMargin; 
     params.topMargin = topMargin; 

     //params.setMargins(leftMargin, topMargin, 0, 0); //this didnt work either 
     infoLayout.setLayoutParams(params); 

     TextView titleView = (TextView) view.findViewById(R.id.titleCard); 
     titleView.setText(title); 

     return view; 
    } 
+0

是infoLayout一个FrameLayout里或RelativeLayout的的子视图? – 2012-03-22 00:04:59

+0

infoLayout是一个RelativeLayout – heero 2012-03-22 00:08:25

+0

为什么使用FrameLayout.LayoutParams代替RelativeLayout.LayoutParams?你有没有尝试过使用RelativeLayout.LayoutParams?更重要的是,你有没有尝试调用params = infoLayout.getParams();然后编辑参数而不是重新创建它们? – 2012-03-22 00:15:57

回答

3

好吧,我能够在另一RelativeLayout的包装我infoLayout来解决这个问题,而现在它完美