【问题标题】:simply switch to another layout with an int只需使用 int 切换到另一个布局
【发布时间】:2014-09-11 08:46:02
【问题描述】:

我想知道是否可以创建一个布局数组,以便能够通过使用简单的 int 切换布局来更改布局。

也许还有其他方法,如果可以,你能给我解释一下吗?

【问题讨论】:

  • WHYTB(你以前试过什么)??!!
  • 只要布局都包含相同的字段,具有相同的资源 ID,我不明白为什么会出现问题。
  • @mansoulx 我没有真正尝试过任何东西......而且解决方案很简单我现在感觉很愚蠢!

标签: android layout android-studio switch-statement


【解决方案1】:

您可以制作布局数组,并且可以根据任何int值进行设置。

// 布局ID列表

int layoutIds[]={R.layout.layout1,R.layout.layout2 ,R.layout.layout3,R.layout.layout4,R.layout.layout5}

//要设置的布局的当前索引。

int layoutIndex=0;

现在在 OnCreate 内部,编写以下代码

setContentView(layoutIds[layoutIndex]);

【讨论】:

  • 这正是我想要的!我觉得有点愚蠢,我以前没有想到这一点!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多