为了能够在编码过程中提高开发效率,使用Activity模板是一个不错的选择,所以,在看了一些教程之后,自己亲自动手练习了一遍,遇到了一些问题在此总结一下,希望对其他的Android开发者起到一定的作用,最终效果如下:
Activity模板效果图.gif这样的模板,在设置页面和关于我们页面是经常会用到的,稍微改改图片、文字、字体大小、字体颜色和分割线颜色就完成了大部分的工作了。
下面介绍下我在写模板的过程中遇到的一些问题
1、不知道在哪儿写
29C17AF3-F709-44E3-8719-F0D910390809.png解决方案:在应用程序中,选中Android Studio.app点击右键,选择显示包内容,最后到达这个位置 Android Studio.app/Contents/plugins/android/lib/templates, 复制一份系统已存在的模板,在此基础上修改即可
2、自己写的模板没有在Android Studio模板列表中显示出来
写模板的时候,我复制了一份EmptyActivity这个模板到当前目录下(与EmptyActivity同目录),重命名文件夹名称为TestActivity,然后重启Android Studio,尝试使用Activity模板时,并没发现TestActivity模板在模板列表中,经过一番周折之后,才知道是复制过来的模板名和EmptyActivity的模板名重复了
解决方案:如下图所示,在这个位置把模板名称改成自己的就可以了
B8797A38-39AE-4B46-BACA-1AFE4516C7E2.png
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" />
网友评论