【问题标题】:how to set only top margin of a layout如何仅设置布局的上边距
【发布时间】:2014-12-11 11:37:34
【问题描述】:

你好我想改变我的布局上边距,但保持其他边距参数不变,我的源在一个片段内,如果我使用下面的代码,我的其他参数将设置为零

       mainScreen = (RelativeLayout) v.findViewById(R.id.main_screen);
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mainScreen.getLayoutParams();
       params.setMargins(0, 390, 0, 0); 
       mainScreen.setLayoutParams(params);

谢谢。

【问题讨论】:

  • 为什么不在xml中设置呢?
  • 来自参考网站 (developer.android.com/reference/android/view/…):A call to requestLayout() needs to be done so that the new margins are taken into account.
  • 我不能在 xlm 上做到这一点,因为在我单击按钮后,该布局会在动画中移动。 IE。布局 1 将覆盖布局 2。当用户单击按钮时,布局 1 移开,这就是为什么我需要更改布局 1 的边距。这两个布局都在 FrameLayout 内。

标签: android layout margin


【解决方案1】:

只需从您从getLayoutParams() 获得的LayoutParams 对象中读取另一个边距

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mainScreen.getLayoutParams();
params.setMargins(params.leftMargin, 390, params.rightMargin, params.bottomMargin); 

【讨论】:

    【解决方案2】:

    您可以使用它来设置仅上边距:

    params.topMargin=10; //It will set value in pixel
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-12
      • 2011-06-16
      • 1970-01-01
      • 2010-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-19
      相关资源
      最近更新 更多