[英]Android - Dynamically name resources inside code
Quiet simply, I am trying to reuse my Adapters and Activity classes. 简单地说,我正在尝试重用我的Adapters和Activity类。 When I call the Intent to open my Activity, I pass name of the Layout I want to inflate as a StringExtra like this:
当我调用Intent打开我的Activity时,我将要膨胀的Layout的名称传递为StringExtra,如下所示:
i.putExtra("layout_name", "layout_a");
Then inside my Activity I obtain the extra: 然后在我的活动中,我获得了额外的收益:
Intent i = getIntent();
String layout_name = i.getStringExtra("layout_name");
Now I want to use this in this format: 现在,我要以这种格式使用它:
setContentView(R.layout. + layout_name);
Is something like this possible? 这样的事情可能吗? What's the correct syntax?
正确的语法是什么?
Something like, 就像是,
int layoutID = getResources().getIdentifier("layout_name" , "layout", getPackageName());
setContentView(layoutID);
You could pass layout identifier: 您可以传递布局标识符:
i.putExtra("layout_id", R.layout.your_layout);
Get this value as 得到这个值
int layout_id = getIntent().getIntExtra("layout_id");
Set this id as 将此ID设置为
setContentView(layout_id);
You can also try Int ID of resource and use it. 您也可以尝试使用资源的Int ID并使用它。
int layout = getIntent().getExtras().getInt("LayoutID");
setContentView(layout);
Hope it Helps!! 希望能帮助到你!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.