2011-11-18 27 views

Trả lời

20

trong phần tử bố cục mẹ của bạn, ví dụ: linearlayout hoặc bất cứ điều gì, chỉ cần thêm android:background="@drawable/background" Điều này sẽ thiết lập nền của bố trí của bạn, giả sử bạn có hình ảnh trong một thư mục/drawable.

+3

Cảm ơn bạn. Tôi cũng đã tìm cách này: linearlyaout.setBackgroundResource (R.drawable.FILENAME); – farissyariati

2

* của nó rất đơn giản qua một hình ảnh trong res/drawable-ldpi/thư mục bất cứ điều gì bạn muốn diaplay và trong xml của bạn viết này.

< 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

Có một cái nhìn tại địa chỉ: android:background

Đây là một xml mẫu của những gì bạn có thể cần phải xây dựng trên,

<?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

Bạn có thể đặt màu nền/drawable trong file xml hoặc từ tệp java.

  1. Thay đổi nền trong tập tin XML

    android:background chịu trách nhiệm cho việc thiết lập nền trong View. Vì vậy, hãy thêm điều này nếu bạn muốn đặt nền cho bất kỳ View nào. Ví dụ. ở trên cùng LinearLayout, RelativeLayout hoặc TextView hoặc Button v.v. Tùy thuộc vào yêu cầu của bạn.

    android: background = "@ drawable/nền"

  2. Thay đổi nền từ file Java

    sử dụng setBackgroundResource để thiết lập nền cho bất kỳ View.

    hoặc đơn giản là nếu bạn muốn thay đổi màu sắc của Xem bạn phải sử dụng setBackgroundColor

0

android: background = "@ drawable/yourBG" trong XML.

yourLayout.setBackgroundResource(resid); 

hoặc yourLayout.setBackgroundDrawable (drawble);

2

Trước tiên, bạn phải đặt hình ảnh của bạn trong background.pngres/drawable/ folder.Later của bạn, bạn phải thiết lập một bố cục mẹ cho bạn TextViewButton widgets.I sẽ xem xét một LinearLayout như một bố cục mẹ cho bạn và mã đi như thế này:

<?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

nhấp chuột phải vào thư mục "Drawable" trong tab Project của bạn. chọn new-> image asset và sau đó chọn hình ảnh của bạn.

Trong mã hóa bố trí của bạn tập tin xml, thiết lập android: background = "@ drawable/background.png"

0

bản sao đầu tiên hình ảnh độ phân giải cao mà bạn muốn thiết lập ở chế độ nền và sau đó qua nó (Làm nhấp chuột phải vào Thư mục có thể vẽ).

sau đó thiết lập tài sản của bố trí như sau android: background = "@ drawable/Yourimagefilename"

0

Bạn cũng có thể thêm hình nền lập trình theo cách này:

getWindow().setBackgroundDrawableResource(R.drawable.your_image); 
Các vấn đề liên quan