且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

为什么 findViewById(R.android.id.home) 总是返回 null?

更新时间:2023-11-06 08:33:46

home"图标是否是一个widget,它是什么类的widget,它的ID是什么(如果有的话),取决于动作条的执行.对于不同的 API 级别,本机操作栏可能会以不同的方式执行此操作,并且所有这些都可能与 appcompat-v7 执行此操作的方式不同.更不用说 ActionBarSherlock 或其他操作栏实现了.

Whether or not the "home" icon is a widget, and what class of widget it is, and what its ID is (if any), is up to the implementation of the action bar. The native action bar may do this differently for different API levels, and all of that may be different than the way appcompat-v7 does it. Let alone ActionBarSherlock or other action bar implementations.

具体来说,android.R.id.home 是一个菜单 ID,这就是为什么你可以在像 onOptionsItemSelected() 这样的地方使用它.它不一定是小部件 ID,这就是为什么它可能会或可能不会与 findViewById() 一起使用.

Specifically, android.R.id.home is a menu ID, which is why you can use it in places like onOptionsItemSelected(). It is not necessarily a widget ID, which is why it may or may not work with findViewById().

理想情况下,您不要试图弄乱不是您自己构建的 UI 的内部实现.

Ideally, you do not attempt to mess with the internal implementation of a UI that you did not construct yourself.

真的需要自己制作向上按钮来设计样式吗?

do one really has to make his own Up button to style it?

我不知道,因为我从来没有尝试过设计它.

I do not know, as I have never tried to style it.