Drawable子類之——InsetDrawable (嵌入)

本文出自 “阿敏其人” 簡書博客,轉載或引用請注明出處。

InsetDrawable對應的標簽是<inset>
他可以將其他的Drawable內嵌到自己的里面。個人覺得其實沒什么用,他能做到的,LayerDrawable也能做,或者有的時候直接設置一下padding就可以了。

一、語法


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

沒什么好說的,四個方向就是想要拉開的距離,比如一個圖片本來應該是占滿屏幕的,然后上下左右個設置為20dp,那么待會這個圖片就不會占滿屏幕,就是距離上下左右都有20dp的距離。

二、demo

insetdrawable_simple.xml

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

    <!--待會要插入的一個藍色的矩形-->
    <shape android:shape="rectangle" >
        <solid android:color="#0000ff" />
    </shape>
</inset>

.
.
布局文件


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"

    tools:context=".MainActivity">

    <!--Inset這東西其實就沒什么用,大概也就這樣子吧,
    背景一個,圖片一個,利用Inset讓背景比較大-->
    <ImageView
        android:id="@+id/mIv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerInParent="true"
        android:background="@mipmap/op"
        android:src="@drawable/insetdrawable_simple"
         />
</RelativeLayout>

.
.
Activity代碼什么都不要動


import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        //InsetDrawable insetDrawable=new InsetDrawable(getResources().getDrawable(R.drawable.XXX), 20, 30, 30, 20);

        
        
    }
}

.
.

demo效果:

demo效果.png

了解更多的Drawable分類 Drawable圖像資源抽象類
本篇完。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容