Android中設定背景圖片平鋪。

來源:互聯網
上載者:User

標籤:

註:本文由Colin撰寫,著作權!轉載請註明原文地址,謝謝合作!

在做Android開發時,我們常常需要為程式設定一個背景,但由於現在的Android裝置尺寸不一,如果隨便設定一個圖片為背景,那麼很可能在不同的裝置上會被展開,給使用者的視覺體驗會很差,那麼如何解決這個問題呢,這裡本菜鳥在此分享一下心得。

 

一、首先,需要在drawable-mdpi目錄裡定義一個xml檔案,在此我命名為bitmap

編寫如下代碼,src中指定為你要平鋪圖片的名稱:

<?xml version="1.0" encoding="utf-8"?><bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/img01" android:id="@+id/app_bg" android:tileMode="repeat" />

二、在layout配置主檔案中將最外的架構背景設定為bitmap即可,如下:

<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/content" android:background="@drawable/bitmap"><TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /></ScrollView>

  

此時,運行虛擬機器看看效果吧。

實現效果如(不含圖中的圓角矩形及半透明效果,圓角矩形的實現請查看下篇文章): 

Demo : 下載Android圓角矩形及半透明演式Demo

 

Android中設定背景圖片平鋪。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.