帮酷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  |  如果智培  |  酷兔英语