有没有办法在另一个资源中包含一个资源(例如多个活动布局中的标题设计)。我知道我可以在运行时添加它,它可以在XML中完成吗?在我的第一个布局资源中包含第二个布局资源
2
A
回答
5
是,你可以在XML做到这一点。见android docs 有关合并/包括
基本上你将有1个布局(root.xml
)如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rootLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="@+id/headingLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<include
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/heading_layout" />
</LinearLayout>
<RelativeLayout>
和heading_layout.xml
:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/titleImg"
android:src="@drawable/bg_cell"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/titleTxt"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</merge>
所以你Activity
你会setContentView(R.layout.root);
这将包括标题。
,你也可以做一些很酷的东西除了这个程序,如XML插入布局到了root.xml
(后setContentView();
:
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.home, rootLayout);
其中rootLayout
是由ID和R.layout.home
发现父RelativeLayout
是您希望添加到根的布局
0
当然可以。退房this后进行了详细的解释:
<include android:id="@+id/my_id" layout="@layout/layout_id" />
相关问题
- 1. 我是否需要每个布局资源的菜单资源
- 2. 无效的布局资源
- 3. Android的布局资源
- 4. WPF店面布局资源
- 5. Android布局资源问题
- 6. 在日食中包含抛出“找不到资源”的布局
- 7. 可以在Android jar中包含布局和其他资源吗?
- 8. Rails 3.1布局文件中包含的所有资源
- 9. 在第二个布局中制作Buttonistener
- 10. 打开第二个抽屉布局作为第一个子抽屉布局
- 11. Xamarin如何通过点击新的资源布局打开另一个布局
- 12. 通过扩展布局的视图包含外部资源
- 13. 常见的布局,不同的资源
- 14. getApplicationContext与来自资源的xml布局
- 15. Rails嵌套资源的嵌套布局
- 16. 使用android不同的布局资源
- 17. 网络专家的XAML布局资源
- 18. 资源特定的设计布局
- 19. 布局读取资源的位置?
- 20. 线性布局错误资源的android:layout_width
- 21. 我包含与另一个布局重叠的坐标布局中的布局
- 22. d3第二个图杀死第一个人的部队布局
- 23. 我怎么能跳一个布局在同一标签布局的第二布局的
- 24. Iphone界面大小/布局资源?
- 25. 用资源布局显示TextView文本
- 26. 如何使用布局,选择资源
- 27. 资源ID定义使用XML布局
- 28. 布局背景资源闪烁进出
- 29. 跳过Android布局xml资源
- 30. 大型Maven资源库布局突出