2010-07-22 34 views

Trả lời

107

sử dụng thuộc tính android:background trong xml của bạn. Cách dễ nhất nếu bạn muốn áp dụng nó cho toàn bộ hoạt động là đặt nó vào thư mục gốc của bố cục của bạn. Vì vậy, nếu bạn có một RelativeLayout như sự khởi đầu của xml của bạn, đặt nó ở đây:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/rootRL" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background"> 
</RelativeLayout> 
+6

Điều này có thể gây ra một số sự cố ... chắc chắn nó sẽ hoạt động cho hoạt động chính của bạn, nhưng nếu bạn mở một hoạt động mới với cùng android: background = "@ drawable/background", ngay sau bạn xoay trình mô phỏng trong hầu hết các trường hợp và một số thiết bị, bạn sẽ gặp sự cố với lỗi "Hết bộ nhớ .." nếu bgImg của bạn có độ phân giải cao. @Sephy, bạn xử lý vấn đề này như thế nào? – whyoz

13

Bạn có thể thiết lập các "hình nền" để một hoạt động bằng cách thiết lập android:background xml thuộc tính như sau:

(Đây Ví dụ, Hãy LinearLayout cho một hoạt động và thiết lập hình nền cho cách bố trí (tức là gián tiếp đến hoạt động))

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout android:id="@+id/LinearLayout01" 
       android:layout_width="fill_parent" 
       android:layout_height="fill_parent" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:background="@drawable/icon"> 
</LinearLayout> 
+1

Tôi đã nhận thấy rằng việc này hoạt động tốt cho LinearLayout, nhưng đối với RelativeLayouts, tôi gặp lỗi OOM. Tôi đã đọc rất nhiều bài đăng decodeBitmap hoặc layout.invalidate() bài đăng, hoặc các bài đăng Bitmap.recycle, v.v. Làm cách nào để bạn vẫn tồn tại một hình ảnh làm nền của ứng dụng bằng cách sử dụng RelativeLayouts? Tôi có nên lưu vào bộ nhớ đệm bất kỳ hình ảnh nào sẽ xuất hiện trong nhiều hoạt động không? Thực hành tốt nhất là gì? – whyoz

0

và không quên để làm sạch dự án của bạn sau khi viết những dòng này `ll một nhận được một lỗi trong tệp xml của bạn cho đến khi yo Bạn đã dọn dẹp dự án của mình trong nhật thực: Project-> Clean ...

2

Đặt hình ảnh trong thư mục có thể vẽ được. thư mục drawable nằm trong res. drawable có 5 biến thể drawable-hdpi drawable-ldpi drawable-mdpi drawable-xhdpi drawable-xxhdpi

+0

Thư mục nào chúng ta phải sử dụng? @Jesvin – jdyg

1

Ngày nay chúng ta phải sử dụng match_parent:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@drawable/background"> 
</RelativeLayout> 

enter image description here

1

Chúng ta có thể dễ dàng đặt hình nền trong PercentFrameLayout bằng cách sử dụng ImageView. Chúng ta phải đặt giá trị thuộc tính scaleType = "fitXY" và trong nền trước, chúng ta cũng có thể hiển thị dạng xem hoặc nút văn bản khác của chế độ xem khác.

<android.support.percent.PercentFrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     xmlns:app="http://schemas.android.com/apk/res-auto" 
     > 
     <ImageView 
      android:src="@drawable/logo" 
      android:id="@+id/im1" 
      android:scaleType="fitXY" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"/> 
<EditText android:layout_gravity="center_horizontal" 
     android:hint="Enter Username" 
     android:id="@+id/et1" 
     android:layout_height="wrap_content" 
     app:layout_widthPercent="50%" 
     app:layout_marginTopPercent="30%" 
     /> 
<Button 
    android:layout_gravity="center_horizontal" 
    android:text="Login" 
    android:id="@+id/b1" 
    android:layout_height="wrap_content" 
    app:layout_widthPercent="50%" 
    app:layout_marginTopPercent="40%"/> 
</android.support.percent.PercentFrameLayout> 
Các vấn đề liên quan