Android的 - 检查是否存在布局 [英] Android - check if layout exists

查看:187
本文介绍了Android的 - 检查是否存在布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法,我可以检查是否使用它的setContentView(R.layout.mylayoutid)?

Is there any way I can check if a certain layout ID exists before using it with setContentView(R.layout.mylayoutid)?

是这样的:

if(layout_exists("mylayoutid"))
    setContentView(R.layout.mylayoutid);
else
    setContentView(R.layout.defaultlayout);

找不到在开发者文档任何东西。

couldn't find anything in the developer docs.

谢谢!

推荐答案

是的,有。

int layoutId = getContext().getResources().getIdentifier("mylayoutid","layout", getContext().getPackageName());
if (layoutId == 0){
  // this layout doesn't exist
} else {
  setContentView(layoutId);
}

这篇关于Android的 - 检查是否存在布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆