矢量圖UI + 9-Patch適配
github:
https://github.com/Gong-Shijie/icon-9-Patch
首先矢量圖網站:
https://www.iconfont.cn/
里面提供了豐富的矢量圖標,供開發者使用
效果:
矢量圖庫
下載圖標 + 導入到Android Studio
圖標
插入矢量圖
Android Studio導入方法
這就是你導入的資源
9-Patch
9-patch圖是可以根據內容來自動適配的圖片資源
可以根據設置的內容來伸縮
利用這一特性可以設計出自適應屏幕時出現的拉伸不適于拉伸部分導致的變形問題
比如我們的message發送的氣泡始終會根據信息長度自動的伸縮
通過png創建9-Patch文件
上下左右四邊
左邊和上邊框用來標記需要被拉伸的區域
右邊和下邊的邊框用來標記內置的內容對齊的區域
使用9-Patch來作為View或者Layout的background可以用來適配不同尺寸的屏幕
主要代碼
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/l1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="20dp"
android:src="@drawable/ic_course"
android:layout_gravity="top|left"
></ImageView>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:src="@drawable/ic_back"></ImageView>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:src="@drawable/ic_0"></ImageView>
</LinearLayout>
<LinearLayout
android:id="@+id/l2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/l1"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/ic_1"
android:layout_marginLeft="20dp"
android:layout_gravity="left"
></ImageView>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="40dp"
android:src="@drawable/ic_2"></ImageView>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="50dp"
android:src="@drawable/ic_3"></ImageView>
</LinearLayout>
<LinearLayout
android:layout_below="@id/l2"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_marginTop="100dp"
android:layout_height="wrap_content">
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_left"
android:text="hello world!">
</TextView>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/message_left"
android:text="這是一條長文本,測試伸縮效果。">
</TextView>
</LinearLayout>
</RelativeLayout>