AndroidStudio插件:布局文件轉(zhuǎn)化Databinding

AndroidStudio插件:布局文件轉(zhuǎn)化Databinding


這幾天一直在做代碼重構(gòu),因為Android是提供Databinding的MVVM架構(gòu),所以就想把代碼里面所有的Butterknife代碼換成Databinding形式。

Github地址

簡介

一個簡單的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>

操作示范

插件下載

DatabindingConvert_v0.1.jar

后續(xù)優(yōu)化

  • 布局代碼轉(zhuǎn)換后,自動格式化代碼
  • 創(chuàng)建帶模板的Databinding文件

版本更新

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

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容