Android 設定背景圖片定位在 View 的左上角

今天有一張小圖如下

 

relativelayout-background-position-02

 

這張圖要定位在某個 TextView 左上角當背景圖 (Layout 也可以)

成果如下圖

relativelayout-background-position-01

 

步驟如下

 

一、

首先,在 /res/drawables/ 底下建立一個背景的 xml 描述檔,取叫 myBackground.xml

路徑就是 /res/drawables/myBackground.xml

 

內容如下面

我的背景圖在 /drawable 裡,叫 my_background,是張 png 圖檔

我希望這張背景圖置於 Layout 的左上角

[sourcecode language=”XML”]

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/my_background"
android:gravity="top|left"/>

[/sourcecode]

 

二、

接著

在 Layout file 裡設定背景為 myBackground.xml

[sourcecode language=”XML”]

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/myBackground"
android:text="測試"/>

[/sourcecode]

資料來源參考 : http://stackoverflow.com/questions/2781593/background-image-placement

6 關於 “Android 設定背景圖片定位在 View 的左上角” 的評論

  1. LED Signs

    Great site. Lots of useful info here. I’m sending it to several pals ans additionally sharing
    in delicious. And of course, thank you in your sweat!

    回覆
  2. รับผลิตอาหารเสริม

    At the current pace of life for many it might be quite difficult to obey a strict regimen of the
    proper, healthy diet. Whenever that takes place you could start munching out and also you
    could turn out eating the ‘wrong foods’.
    You should also purchase products from manufacturers with good reputations
    and ones that have been in operation for a lot of time.

    回覆
  3. puma sneakers sale

    An outstanding share! I’ve just forwarded this onto a co-worker who has been doing
    a little research on this. And he actually bought me lunch due to the fact that I
    stumbled upon it for him… lol. So allow me to reword this….
    Thank YOU for the meal!! But yeah, thanks for spending the
    time to talk about this subject here on your web site.

    回覆
  4. Durand & Smith Designs

    Αn outstanding sharе! I have just forwarded this ontօ a friend ᴡho has
    been conducting a little research on this. And he actually Ƅought me luncһ because
    I ѕtumƄled upon it foг him… lol. So allⲟw me to reword
    thiѕ…. Thanks foг the meal!! But yeaһ, thanks for spending some time
    to talk aƄout this topic here on үour
    site.

    回覆

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *