【问题标题】:Android: Is it possible to make a Contentview dynamic?Android:是否可以使 Contentview 动态化?
【发布时间】:2010-11-18 19:12:18
【问题描述】:

是否可以只创建一个类并为 contentview 使用一个变量?我正在考虑创建一个带有大量带有 ID 的按钮的主布局。一旦按下按钮,它就会将 ID 传递给类并使用该 ID 打开特定的布局。一个类,多种布局。

public class Main extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //is it possible to make 'main' a variable?
    }
}

【问题讨论】:

  • 你得到答案了吗?

标签: android class variables layout


【解决方案1】:

R.layout.main 解析为整数(或者可能是长整数,我忘记了),因此您应该能够设置某种控制结构,如下所示:

switch (mSomeDeterminingFactor) {
    case 1:
        layoutChoice = R.layout.main
        break;
    case 2:
        layoutChoice = R.layout.altMain1
        break;
    case 3:
        layoutChoice = R.layout.altMain2
        break;
    case else:
        layoutChoice = R.layout.main
        break;
 }
 setContentView(layoutChoice);

编辑:你应该小心你以后如何使用/保存/存储这个数字,因为你不能保证资源值是相同的。您应该继续通过 R 类引用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-10-14
    • 1970-01-01
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-18
    相关资源
    最近更新 更多