有沒有辦法在另一個資源中包含一個資源(例如多個活動佈局中的標題設計)。我知道我可以在運行時添加它,它可以在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資源庫佈局突出