Android Drawable Resource學習(八)、InsetDrawable

來源:互聯網
上載者:User

InsetDrawable 表示一個drawable嵌入到另外一個drawable內部,並且在內部留一些間距,這一點很像drawable的padding屬性,區別在於 padding表示drawable的內容與drawable本身的邊距,insetDrawable表示兩個drawable和容器之間的邊距。當控制項需要的背景比實際的邊框小的時候比較適合使用InsetDrawable。

檔案位置:
res/drawable/filename.xml
檔案 名即資源ID
編譯資源類型:

指向InsetDrawable的指標

資源引用:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
文法:
<?xml version="1.0" encoding="utf-8"?><inset    xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/drawable_resource"    android:insetTop="dimension"    android:insetRight="dimension"    android:insetBottom="dimension"    android:insetLeft="dimension" />
元素
<inset>
定義這個drawable為InsetDrawable,必須作為根節點。

屬性:

xmlns:android
String類型。必須的,定義XML檔案的命名空間,必須是 "http://schemas.android.com/apk/res/android".
android:drawable
Drawable 資源 。必須的。引用一個drawable資源
android:insetTop
尺寸。與頂部的距離。可以使一個尺寸值,或者一個尺寸的資源。
android:insetRight
尺寸。與右邊的距離。可以使一個尺寸值,或者一個尺寸的資源。
android:insetBottom
尺寸。與底部的距離。可以使一個尺寸值,或者一個尺寸的資源。
android:insetLeft
尺寸。與左邊的距離。可以使一個尺寸值,或者一個尺寸的資源。
例子:
<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"    android:drawable="@drawable/background"    android:insetTop="10dp"    android:insetLeft="10dp" />
參考:
  • InsetDrawable

執行個體:

1、在xml檔案中定義

inset.xml:

<?xml version="1.0" encoding="utf-8"?><inset xmlns:android="http://schemas.android.com/apk/res/android"     android:drawable="@drawable/image4"    android:insetLeft="50dp"    android:insetRight="50dp"    android:insetTop="20dp"    android:insetBottom="20dp"></inset>

layout中使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"      android:background="@drawable/inset" >     <Button        android:id="@+id/btn"         android:layout_width="wrap_content"        android:layout_height="wrap_content"/></LinearLayout>

可以看到,它與邊界之間是有間距的。就是我們在inset.xml中所定義的。

2、java代碼定義:

很簡單:

//new InsetDrawable(drawable, insetLeft, insetTop, insetRight, insetBottom) InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.image4), 20, 30, 30, 20);        

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.