Android_ViewStub

主窗体xml:

[html] view
plain
copy

  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity" >
  6. <com.yzh.lockpri2.widget.ScaledImageView
  7. android:id="@+id/imageBG"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"/>
  10. <FrameLayout
  11. android:layout_width="match_parent"
  12. android:layout_height="100dp"
  13. android:paddingBottom="5dp">
  14. <com.yzh.lockpri2.widget.BluredImageView
  15. android:id="@+id/headerBg"
  16. android:layout_width="match_parent"
  17. android:layout_height="match_parent"/>
  18. <TextView
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:text="@string/welcome_title"
  22. android:layout_gravity="center"
  23. style="@style/welcome_bold_info_font"
  24. />
  25. <TextView
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:text="@string/welcome_info"
  29. style="@style/welcome_little_info_font"
  30. android:layout_gravity="center_horizontal|bottom"
  31. android:layout_marginBottom="3dp"/>
  32. </FrameLayout>
  33. <ViewStub
  34. android:id="@+id/keyboardStub"
  35. android:layout_width="match_parent"
  36. android:layout_height="match_parent"
  37. android:layout_gravity="bottom"
  38. android:inflatedId="@+id/keyboard"
  39. layout="@layout/keyboard"
  40. />
  41. </FrameLayout>

keyboard是另外一个xml中的布局, 在视图中能正常显示.

然后在代码中:

[java] view
plain
copy

  1. stub = (ViewStub)findViewById(R.id.keyboardStub);
  2. stub.inflate();

程序执行到 stub.inflate(); 报错 "ViewStub must have a valid layoutResource". 检查了半天,

才发现是在主xml中设置时使用了<include>标签的用法:

[html] view
plain
copy

  1. layout="@layout/keyboard"

正确的应该是:

[html] view
plain
copy

  1. android:layout="@layout/keyboard"

修改后, 执行正常!

时间: 2024-08-28 09:36:54

Android_ViewStub的相关文章

New UI-&lt;ViewStub&gt;标签延时加载布局

New UI-<ViewStub>标签延时加载布局  --转载请注明出处:coder-pig,欢迎转载,请勿用于商业用途! 小猪Android开发交流群已建立,欢迎大家加入,无论是新手,菜鸟,大神都可以,小猪一个人的 力量毕竟是有限的,写出来的东西肯定会有很多纰漏不足,欢迎大家指出,集思广益,让小猪的博文 更加的详尽,帮到更多的人,O(∩_∩)O谢谢! 小猪Android开发交流群:小猪Android开发交流群群号:421858269 新Android UI实例大全目录:http://blog