帮酷LOGO
0 0 评论
文章标签:template  TEMP  文件  Android Studio  

问题

我决定为fragment创建一个模板,但是,在哪里做?

首先,我在Android Studio中提取了IDE设置。我找到了列出各种模板的区域,我以为我找到了,我创建了一个名为"fragment lifecycle的新模板,并用所需的内容填充它,然后我应用了更改并关闭了窗口。

我右键点击我的fragments package> new> ......没有任何改变。事实证明,我创建的是一个通用的Java模板。我可以通过转到new但是,这显然并不理想,因为它没有挂钩到模板的Android区域。另外,对于需要布局资源的模板,我将来会做些什么?我想要做到这一点。

Android模板

在一些搜索之后,我发现在Android Studio中使用的Android模板并不特定用在Android Studio ,事实上,他们甚至都不是IntelliJ ,相反,这些模板是hook的,并且基于一个叫做FreeMarker的系统,这是在Eclipse中使用ADT插件提供模板的模板技术。

Android模板位置

你可以在以下位置找到所有Android Studio android模板:
ANDROID_STUDIO_DIR/plugins/android/lib/templates

Android模板结构&格式

你可以在这里了解如何对模板进行结构和格式设置:
Android ADT模板格式文档

模板示例

我创建了一个名为"Lifecycle Fragment"的新模板,并将它放置在上面描述的路径,这些文件的内容如下所示,以了解模板的外观。

目录结构为:

 

FragmentWithLifecycle
 . /template.xml
 . /recipe.xml.ftl
 . /globals.xml.ftl
 . /temlpate_blank_fragment.png
 . /root/src/app_package/LifecycleFragment.java.ftl
 . /root/res/layout/fragment_blank.xml.ftl

template.xml

 

<?xml version= "1.0" ?>
<template
 format= "4" 
 revision= "1" 
 name= "New Fragment With Lifecycle" 
 minApi= "7" 
 minBuildApi= "8" 
 description= "Creates a new fragment, with hooks for the entire fragment lifecycle and a static initializer."> 

 <dependency name= "android-support-v4" revision= "8" />

 <category value= "Other" />

 <parameter
 id= "className" 
 name= "Fragment Name" 
 type= "string" 
 constraints= "class|unique|nonempty" 
 default= "MyFragment" 
 help= "The name of the fragment class to create" />

 <parameter
 id= "includeLayout" 
 name= "Create layout XML?" 
 type= "boolean" 
 default= "true" 
 help= "Generate a layout XML for the fragment" />

 <thumbs>
 <thumb>template_blank_fragment.png</thumb>
 </thumbs>

 <globals file= "globals.xml.ftl" />
 <execute file= "recipe.xml.ftl" />

</template>

recipe.xml.ftl

 

<?xml version= "1.0" ?>
<recipe>
 <dependency mavenUrl= "com.android.support:support-v4:+" />

 <#if includeLayout>
 <instantiate from= "res/layout/fragment_blank.xml.ftl" 
 to= "${escapeXmlAttribute(resOut)}/layout/fragment_${classToResource(className)}.xml" />

 <open file= "${escapeXmlAttribute(resOut)}/layout/fragment_${classToResource(className)}.xml" />
 </#if>

 <open file= "${escapeXmlAttribute(srcOut)}/${className}.java" />

 <instantiate from= "src/app_package/LifecycleFragment.java.ftl" 
 to= "${escapeXmlAttribute(srcOut)}/${className}.java" />
</recipe>

globals.xml.ftl

 

<?xml version= "1.0" ?>
<globals>
 <global id= "resOut" value= "${resDir}" />
 <global id= "srcOut" value= "${srcDir}/${slashedPackageName(packageName)}" />
</globals>

template_ blank _fragment.png

从预先存在的空白fragment模板目录中复制。

root/src/app_package/LifecycleFragment.java.ftl

 

package ${packageName};

import <#if appCompat?has_content>android.support.v7.app.ActionBarActivity<#else>android.app.Activity</#if>;
import android.<#if appCompat?has_content>support.v4.</#if>app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class ${className} extends Fragment {

 //public static final String arg_param1 = "SOMETHING" ;

 public static ${className} newInstance()
 {
 ${className} frag = new ${className}();

 //Get arguments pass in, if any
 Bundle args = frag.getArguments();
 if(args == null)
 {
 args = new Bundle();
 }

 //Add parameters to the arguments bundle
 //args.putInt(SOME_KEY, someValue);

 frag.setArguments(args);

 return frag;
 }

 public ${className}()
 {
 //TODO
 }

 //------- Start Fragment Lifecycle -----------
 @Override
 public void onAttach(Activity activity)
 {
 super.onAttach(activity);
 }

 @Override
 public void onCreate(Bundle savedInstanceState)
 {
 super.onCreate(savedInstanceState);
 }

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
 {
<#if includeLayout>
 //Inflate the layout for this fragment
 return inflater.inflate(R.layout.fragment_${classToResource(className)}, container, false);
<#else>
 return super.onCreateView(inflater, container, savedInstanceState);
</#if>
 }

 @Override
 public void onActivityCreated(Bundle savedInstanceState)
 {
 super.onActivityCreated(savedInstanceState);
 }

 @Override
 public void onStart()
 {
 super.onStart();
 }

 @Override
 public void onResume()
 {
 super.onResume();
 }

 @Override
 public void onPause()
 {
 super.onPause();
 }

 @Override
 public void onStop()
 {
 super.onStop();
 }

 @Override
 public void onDestroyView()
 {
 super.onDestroyView();
 }

 @Override
 public void onDestroy()
 {
 super.onDestroy();
 }

 @Override
 public void onDetach()
 {
 super.onDetach();
 }
 //------- End Fragment Lifecycle -------------
}

root/res/layout/fragment_blank.xml.ftl

 

<LinearLayout xmlns:android= "http://schemas.android.com/apk/res/android" 
 android:layout_width= "wrap_content" 
 android:layout_height= "wrap_content" 
 android:orientation= "vertical" 
> 

</LinearLayout>


文章标签:文件  TEMP  template  Android Studio  

Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语