【问题标题】:Why I can not set a custom size for my relativeLayout object为什么我不能为我的 relativeLayout 对象设置自定义大小
【发布时间】:2015-11-30 22:21:40
【问题描述】:

在下面的代码中,为什么我不能为我的relativeLayout 对象设置自定义大小?我设备的分辨率是 240 * 320。

RelativeLayout relativeLayout;
    @Override
    protected void onCreate(Bundle bundle){
            super.onCreate(bundle);
    relativeLayout = new RelativeLayout(this);
    relativeLayout.getLayoutParams().width=240;
        relativeLayout.getLayoutParams().height=320;
    setContentView(relativeLayout);}

【问题讨论】:

  • 为什么不使用 MATCH_PARENT?
  • 我可以使用match_parent。但我想要这个问题的科学原因。
  • stackoverflow.com/questions/3224193/… 并确保您的屏幕尺寸实际上是 240px(而不是 dp)

标签: android


【解决方案1】:

您应该尝试使用这样的布局参数:

RelativeLayout relativeLayout = new RelativeLayout(this);
//You can put the background in another color to check were is the layout
//relativeLayout.setBackgroundResource(android.R.color.black);

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(240, 320);
relativeLayout.setLayoutParams(lp);

setContentView(relativeLayout);

【讨论】:

    猜你喜欢
    • 2011-02-11
    • 2020-08-19
    • 1970-01-01
    • 2020-05-11
    • 2021-04-11
    • 1970-01-01
    • 2014-09-22
    • 2021-11-13
    • 1970-01-01
    相关资源
    最近更新 更多