我是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:如何在表格佈局中設置背景圖片
- 3. android - 從url設置佈局背景
- 4. 如何在xml佈局中設置背景顏色?
- 5. 如何在相對佈局背景中設置位圖圖像
- 6. 從Uri設置佈局的背景
- 7. 如何在Android中設置ImageView背景
- 8. 如何在COCOS2D android中設置背景?
- 9. 如何在Android中通過Url設置佈局的背景圖像?
- 10. 如何在Android中爲佈局背景設置不透明度(Alpha)?
- 11. 如何設置畫布作爲佈局的背景
- 12. 如何使用庫中的照片設置佈局的背景?
- 13. Android。佈局背景延伸
- 14. 的Android:佈局背景
- 15. 如何在android中設置此佈局?
- 16. 在android中設置佈局
- 17. 適當的方式來設置佈局的背景圖像android
- 18. Android:設置使用圖像路徑的佈局背景
- 19. Android:將佈局設置爲視圖的背景
- 20. 如何正確設置「alert_light_frame」作爲背景到佈局?
- 21. 如何動態設置相對佈局背景圖片?
- 22. 如何設置不透明的圖片作爲佈局背景
- 23. Android設置背景
- 24. 如何在Android中拖動佈局時動態更改佈局背景?
- 25. Android佈局中的背景延伸
- 26. 設置圓背景Android中
- 27. 在mvc佈局頁面中設置背景圖片
- 28. 如何在Android佈局中更改背景
- 29. 如何把UIImageView放在背景佈局
- 30. 設置HTML畫布背景
謝謝你。我也是這樣找到的: linearlyaout.setBackgroundResource(R.drawable.FILENAME); – farissyariati