2011-11-18 77 views

回答

20

在您的父級佈局元素中,例如linearlayout或其他,只需添加android:background="@drawable/background"這將設置你的佈局的背景,假設你有一個/可繪製文件夾中的圖像。

+3

謝謝你。我也是這樣找到的: linearlyaout.setBackgroundResource(R.drawable.FILENAME); – farissyariati

2

*其非常簡單的 通過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> 
1

看一看:android:background

這是你可能需要建立在什麼樣的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> 
1

您可以設置背景顏色/繪製在XML文件或從java文件。

  1. 在XML文件

    android:background變化的背景是負責查看設置背景。 因此,如果您想將背景設置爲任何View,請添加此項。例如。在頂部LinearLayoutRelativeLayoutTextViewButton等。取決於您的要求。

    的android:背景= 「@繪製/背景」

  2. 從Java文件更改背景

    使用setBackgroundResource設置背景的任何視圖。

    或只是如果你想改變觀看的顏色,你必須使用setBackgroundColor

0

的android:背景= 「@繪製/ yourBG」 中的XML。

yourLayout.setBackgroundResource(resid); 

或 yourLayout.setBackgroundDrawable(drawble);

2

首先,你必須把你的background.png形象在你的res/drawable/ folder.Later你必須設置父佈局爲您TextViewButton 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> 
0

首先點擊右鍵。 選擇新圖片資源,然後選擇您的圖片。

在XML文件中的佈局編碼,設置機器人:背景=「@可繪製/ background.png」

0
要設置背景,然後過去,(做對

第一個副本高分辨率圖像點擊可繪製文件夾)。

然後設置佈局的屬性如下 機器人:背景=「@繪製/ Yourimagefilename」

0

您還可以添加背景圖像編程方式是這樣的:

getWindow().setBackgroundDrawableResource(R.drawable.your_image);