為了能夠在編碼過程中提高開發效率,使用Activity模板是一個不錯的選擇,所以,在看了一些教程之后,自己親自動手練習了一遍,遇到了一些問題在此總結一下,希望對其他的Android開發者起到一定的作用,最終效果如下:
這樣的模板,在設置頁面和關于我們頁面是經常會用到的,稍微改改圖片、文字、字體大小、字體顏色和分割線顏色就完成了大部分的工作了。
下面介紹下我在寫模板的過程中遇到的一些問題
1、不知道在哪兒寫
解決方案:在應用程序中,選中Android Studio.app點擊右鍵,選擇顯示包內容,最后到達這個位置 Android Studio.app/Contents/plugins/android/lib/templates, 復制一份系統已存在的模板,在此基礎上修改即可
2、自己寫的模板沒有在Android Studio模板列表中顯示出來
寫模板的時候,我復制了一份EmptyActivity這個模板到當前目錄下(與EmptyActivity同目錄),重命名文件夾名稱為TestActivity,然后重啟Android Studio,嘗試使用Activity模板時,并沒發現TestActivity模板在模板列表中,經過一番周折之后,才知道是復制過來的模板名和EmptyActivity的模板名重復了
解決方案:如下圖所示,在這個位置把模板名稱改成自己的就可以了
3、使用模板創建出來的布局文件是這樣的
<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="${packageName}.${activityClass}">
<!--我的積分 我的余額 我的收貨地址 我的收藏 積分商城 我的優惠券-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/px10dp"
android:layout_marginTop="@dimen/px10dp"
android:background="@color/white"
android:divider="@drawable/insert_mine_fragment_divider"
android:orientation="vertical"
android:showDividers="middle">
<LinearLayout
android:id="@+id/ll_my_integral"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
style="@style/mine_fragment_item_style"
android:layout_width="@dimen/px0dp"
android:layout_weight="1"
android:background="@color/white"
android:clickable="false"
android:drawableLeft="@drawable/mine_integral"
android:drawableRight="@null"
android:text="我的積分" />
<TextView
android:id="@+id/tv_my_integral"
style="@style/mine_fragment_item_style"
android:layout_width="wrap_content"
android:background="@color/white"
android:clickable="false"
android:drawableLeft="@null"
android:text="56"
android:textColor="@color/color_ee2222" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll_my_balance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:orientation="horizontal">
<TextView
style="@style/mine_fragment_item_style"
android:layout_width="@dimen/px0dp"
android:layout_weight="1"
android:background="@color/white"
android:clickable="false"
android:drawableLeft="@drawable/mine_integral"
android:drawableRight="@null"
android:text="我的余額" />
<TextView
android:id="@+id/tv_my_balance"
style="@style/mine_fragment_item_style"
android:layout_width="wrap_content"
android:background="@color/white"
android:clickable="false"
android:drawableLeft="@null"
android:text="98.00"
android:textColor="#ee2222" />
</LinearLayout>
<TextView
android:id="@+id/tv_my_address"
style="@style/mine_fragment_item_style"
android:drawableLeft="@drawable/mine_integral"
android:text="我的地址" />
<TextView
android:id="@+id/tv_my_collection"
style="@style/mine_fragment_item_style"
android:drawableLeft="@drawable/mine_integral"
android:text="我的收藏" />
</LinearLayout>
</RelativeLayout>
很明顯的一句代碼會報錯
tools:context="${packageName}.${activityClass}"
原因是:模板中的布局文件以.xml結尾且在recipe.xml.ftl文件中使用copy關鍵字把模板中的布局文件復制到項目中的布局文件中,因此,導致以上問題的發生。
解決方案:
在布局文件名尾部加上.ftl,然后在recipe.xml.ftl文件中使用instantiate關鍵字即可,例如:
<instantiate from="res/layout/activity_test.xml.ftl"
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
4、最后提供下recipe.xml.ftl文件中常用的一些代碼及說明
注意:from后天的名稱一定要和模板中的一致
4.1合并strings.xml
<merge from="res/values/strings.xml.ftl"
to="${escapeXmlAttribute(resOut)}/values/strings.xml" />
4.2合并colors.xml
<merge from="res/values/colors.xml.ftl"
to="${escapeXmlAttribute(resOut)}/values/colors.xml" />
4.3合并styles.xml
<merge from="res/values/styles.xml.ftl"
to="${escapeXmlAttribute(resOut)}/values/styles.xml" />
4.3復制模板中的圖片到項目中的drawable-xhdpi中
<copy from="res/drawable-xhdpi"
to="${escapeXmlAttribute(resOut)}/drawable-xhdpi" />
4.4初始化布局文件
<instantiate from="res/layout/activity_test.xml.ftl"
to="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />
4.5使用模板創建完成后,默認打開的文件
<open file="${escapeXmlAttribute(srcOut)}/${activityClass}.java" />
<open file="${escapeXmlAttribute(resOut)}/layout/${layoutName}.xml" />