0

我已经使用 RelativeLayout 创建了一个布局,但是当方向更改为 Landscape 时,我需要更改上边距。

   @Override
  public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);

   // Checks the orientation of the screen
   if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {

       RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
      // labelLayoutParams.setMargins(0, -100, 0, 0);
       layout.setLayoutParams(labelLayoutParams);

       Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
   } 

但我得到以下异常

 06-25 22:22:31.166: E/AndroidRuntime(8211): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams

所以请帮助如何设置布局参数,特别是相对布局的上边距?程序化地

4

2 回答 2

0

尝试这个:

RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
labelLayoutParams.topMargin = 10;
layout.setLayoutParams(labelLayoutParams);
于 2012-06-25T18:34:32.427 回答
0

按照设置视图的绝对位置尝试这种方式

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);

    params.topMargin = 60;

    layout.setLayoutParams(params);
于 2012-06-25T18:36:59.903 回答