我应该调用什么方法来知道Activity是否有其contentView(一旦调用了方法setContentView())?如何获取Activity的内容视图?
回答
this.getWindow().getDecorView().findViewById(android.R.id.content)
或
this.findViewById(android.R.id.content)
或
this.findViewById(android.R.id.content).getRootView()
您可能想试试View.getRootView()
。
如果您将ID添加到您的布局中,您可以获取视图。
<RelativeLayout
android:id="@+id/my_relative_layout_id"
而且从findViewById叫它...
这是获取已安装布局的根的最稳健的方式。 – 2013-08-26 23:47:23
你将什么传递给'findViewById'? – trans 2016-06-16 15:12:57
@trans查看contentView = findViewById(R.id.my_relative_layout_id); – 2017-01-13 03:04:42
您还可以覆盖onContentChanged()
这其中包括发射时setContentView()
被调用。
ernest的答案会给你当前的焦点视图,即使包含可滚动视图 – YuDroid 2012-09-08 14:06:56
没有“isContentViewSet”方法。你可以把一些虚拟的requestWindowFeature呼叫到try/catch块的setContentView之前是这样的:
try { requestWindowFeature(Window.FEATURE_CONTEXT_MENU); setContentView(...) } catch (AndroidRuntimeException e) { // do smth or nothing }
如果内容视图已经设置,requestWindowFeature会抛出异常。
如何
View view = Activity.getCurrentFocus();
我找到的最好的选项和较少干扰,是设置标签PARAM你xml,如
PHONE XML布局
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="phone"/>
TABLET XML布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="tablet">
...
</RelativeLayout>
,然后在活动类称之为:
View viewPager = findViewById(R.id.pager);
Log.d(getClass().getSimpleName(), String.valueOf(viewPager.getTag()));
希望工程妳。
这是如何回答这个问题? – Onik 2014-05-06 01:02:30
- 1. Android如何为整个Activity主内容视图注册OnTouchEvent?
- 2. 如何从不扩展Activity的类获取视图引用?
- 3. 如何从控制器中的视图获取Html内容?
- 4. 如何获取块的内容以显示在视图中?
- 5. 获取滚动视图内的视图
- 6. 如何获取href内容
- 7. framelayout如何获取内容?
- 8. 如何获取内容?
- 9. 获取窗口的视觉内容
- 10. 如何更改GLSurfaceView的内容视图?
- 11. 如何重绘UIScrollView的内容视图
- 12. 如何获取我的JTextPane的内容?
- 13. 如何保存android内容视图的修改内容?
- 14. 如何获得视图的内容大小
- 15. 如何获取网格视图内图像的位置?
- 16. 如何获取ComboBox.SelectedItem的内容?
- 17. 如何获取HTML标签的内容?
- 18. 如何获取ScrolledText的内容?
- 19. 如何获取StackPanel的内容高度?
- 20. Javascript:如何获取网址的内容?
- 21. 如何获取Java库中的内容?
- 22. 如何获取已阅读的内容
- 23. 如何获取PUT请求的内容?
- 24. 如何获取div的内容高度
- 25. 如何内容获取:从WordPress的RSS
- 26. 如何获取RichTextBox的内容?
- 27. 如何使用JavaScript获取的内容?
- 28. 如何获取子div的内容javascript
- 29. 如何获取该内容的大小
- 30. 如何获取文件内容的ParcelFileDescriptor?
'findViewById(android.R.id.content);'工作正常 – Blundell 2012-02-18 15:42:53
或'getWindow()。findViewById(android.R.id.content)': ) – 2013-06-26 01:54:33
这个工作正常,只要提取父视图有关... BUt ..如果我尝试 this.findViewById(android.R.id.content).setBackgroundDrawable(d); it剂量工作.. 你能请建议.. 在此先感谢 – 2013-08-02 09:08:56