我应该调用什么方法来知道一个活动是否有它的contentView(一旦方法setContentView()被调用)?


当前回答

没有“isContentViewSet”方法。你可以把一些伪requestWindowFeature调用放到setContentView之前的try/catch块中,像这样:

try {
  requestWindowFeature(Window.FEATURE_CONTEXT_MENU);
  setContentView(...)
} catch (AndroidRuntimeException e) {
  // do smth or nothing
}

如果已经设置了内容视图,requestWindowFeature将抛出异常。

其他回答

如何

View view = Activity.getCurrentFocus();

您可能想尝试View.getRootView()。

如果您正在使用Kotlin

    this.findViewById<View>(android.R.id.content)
                         .rootView
                         .setBackgroundColor(ContextCompat.getColor(this, R.color.black))
this.getWindow().getDecorView().findViewById(android.R.id.content)

or

this.findViewById(android.R.id.content)

or

this.findViewById(android.R.id.content).getRootView()

没有“isContentViewSet”方法。你可以把一些伪requestWindowFeature调用放到setContentView之前的try/catch块中,像这样:

try {
  requestWindowFeature(Window.FEATURE_CONTEXT_MENU);
  setContentView(...)
} catch (AndroidRuntimeException e) {
  // do smth or nothing
}

如果已经设置了内容视图,requestWindowFeature将抛出异常。