AndroidStudio插件:布局文件轉(zhuǎn)化Databinding
序
這幾天一直在做代碼重構(gòu),因為Android是提供Databinding的MVVM架構(gòu),所以就想把代碼里面所有的Butterknife代碼換成Databinding形式。
簡介
一個簡單的Android Studio 插件,把當(dāng)前的布局文件轉(zhuǎn)化成Databinding的形式。
如,當(dāng)一個布局文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<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.support.design.widget.TabLayout
android:id="@+id/tabLayout"
style="@style/tabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary"/>
<android.support.v4.view.ViewPager
android:id="@+id/vpMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
轉(zhuǎn)化過就是這樣(注意:還沒有實現(xiàn)代碼轉(zhuǎn)化后的格式化,需要手動格式化):
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="" />
<variable
name=""
type="" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
style="@style/tabLayoutStyle"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorPrimary" />
<android.support.v4.view.ViewPager
android:id="@+id/vpMainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
操作示范
插件下載
后續(xù)優(yōu)化
- 布局代碼轉(zhuǎn)換后,自動格式化代碼
- 創(chuàng)建帶模板的Databinding文件
版本更新
- v0.0.1 完成基本的布局轉(zhuǎn)換