我是Android編程的新手。我有一個UI和一些TextView
和Button
控件。我如何在這些組件背後設置背景?我們稱之爲background.png
。如何在Android UI中設置佈局背景
回答
在您的父級佈局元素中,例如linearlayout或其他,只需添加android:background="@drawable/background"
這將設置你的佈局的背景,假設你有一個/可繪製文件夾中的圖像。
*其非常簡單的 通過res/drawable-ldpi /文件夾中的圖像無論您想要在哪裏顯示,並在您的xml中寫入此內容。
< LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myview"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:background="@drawable/icon">
< Button/>
< TextView />
</LinearLayout>
這是你可能需要建立在什麼樣的XML,
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:id="@+id/my_view"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background">
</FrameLayout>
您可以設置背景顏色/繪製在XML文件或從java文件。
在XML文件
android:background
變化的背景是負責查看設置背景。 因此,如果您想將背景設置爲任何View
,請添加此項。例如。在頂部LinearLayout
,RelativeLayout
或TextView
或Button
等。取決於您的要求。的android:背景= 「@繪製/背景」
從Java文件更改背景
使用
setBackgroundResource
設置背景的任何視圖。或只是如果你想改變觀看的顏色,你必須使用
setBackgroundColor
的android:背景= 「@繪製/ yourBG」 中的XML。
yourLayout.setBackgroundResource(resid);
或 yourLayout.setBackgroundDrawable(drawble);
首先,你必須把你的background.png
形象在你的res/drawable/
folder.Later你必須設置父佈局爲您TextView
和Button
widgets.I將考慮LinearLayout
爲你父母的佈局和代碼是這樣的:在你的項目選項卡「繪製對象」文件夾
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/background.png"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:text="Button"
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
</LinearLayout>
首先點擊右鍵。 選擇新圖片資源,然後選擇您的圖片。
在XML文件中的佈局編碼,設置機器人:背景=「@可繪製/ background.png」
第一個副本高分辨率圖像點擊可繪製文件夾)。
然後設置佈局的屬性如下 機器人:背景=「@繪製/ Yourimagefilename」
您還可以添加背景圖像編程方式是這樣的:
getWindow().setBackgroundDrawableResource(R.drawable.your_image);
- 1. 如何在Android中設置不同類的背景/佈局
- 2. android - 從url設置佈局背景
- 3. 如何在xml佈局中設置背景顏色?
- 4. 如何在相對佈局背景中設置位圖圖像
- 5. 如何在Android中爲佈局背景設置不透明度(Alpha)?
- 6. 如何在Android中通過Url設置佈局的背景圖像?
- 7. Android。佈局背景延伸
- 8. 的Android:佈局背景
- 9. 如何在android中設置此佈局?
- 10. Android:將佈局設置爲視圖的背景
- 11. 適當的方式來設置佈局的背景圖像android
- 12. Android設置背景
- 13. 如何動態設置相對佈局背景圖片?
- 14. 如何正確設置「alert_light_frame」作爲背景到佈局?
- 15. 在mvc佈局頁面中設置背景圖片
- 16. 如何在Android佈局中更改背景
- 17. 如何把UIImageView放在背景佈局
- 18. 設置HTML畫布背景
- 19. 如何將Android佈局的背景設置爲顏色和圖像?
- 20. 佈局佈局,頂部佈局的中心背景圖片
- 21. 無法爲線性佈局設置背景資源
- 22. 如何防止jQuery UI對話框修改背景佈局?
- 23. Android設置佈局
- 24. Android:如何在線性佈局內設置網格佈局?
- 25. Android風景應用中的佈局UI如何
- 26. 設置在android中透明背景alertdialog
- 27. 在OpenGL中設置背景android
- 28. 在活動佈局後面設置默認背景
- 29. 如何將TextView與佈局中的背景對齊 - Android
- 30. Android查看背景佈局問題
謝謝你。我也是這樣找到的: linearlyaout.setBackgroundResource(R.drawable.FILENAME); – farissyariati