【问题标题】:How do i pass a string to setContentView()? [duplicate]如何将字符串传递给 setContentView()? [复制]
【发布时间】:2016-05-07 02:25:31
【问题描述】:

我的应用程序有一个使用方法

setContentView(R.layout.activity_deashibarai);

设置活动的视图。

但我希望将 xml 文件名(在本例中为 activity_deashibarai)作为变量传递。 我有一个字符串变量来存储该变量。

setContentView(R.layout.variable) 不起作用。

【问题讨论】:

    标签: android string variables setcontentview


    【解决方案1】:

    这解决了。

        Bundle extras = getIntent().getExtras();
        String xmlfile = null;
        if (extras != null) {
            xmlfile = extras.getString("xmlfile");
        }
    
    
        int id = getResources().getIdentifier(xmlfile, "layout", getPackageName());
        setContentView(id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-02
      • 2010-11-28
      • 1970-01-01
      • 2023-03-12
      • 2015-03-20
      • 2012-12-04
      • 1970-01-01
      • 2016-10-25
      相关资源
      最近更新 更多