2

AndroidStudio更换xml默认的布局

 2 years ago
source link: https://www.kymjs.com/note/2018/09/09/01/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

AndroidStudio更换xml默认的布局

2018-09-09 By 张涛 | 本文已被访问1157次

版权声明: 本文是开源实验室原创文章,如您转载必须以链接形式注明原文地址:https://kymjs.com/note/2018/09/09/01
对本知识点有任何问题,可加我的个人微信:kymjs123

自从 AndroidStudio 到 2.3 了以后,新建一个 Activity,默认的布局就从以前的 LinearLayout 变成了 Google 强推的 ConstraintLayout,但其实这个布局是可以被修改的。

在 Mac 上改默认布局(Windows可以自己找对应的目录),需要编辑这个文件:

/Applications/Android\ Studio.app/Contents/plugins/android/lib/templates/activities/common/root/res/layout/simple.xml.ftl   

同时附赠一个 AndroidStudio 中使用 Kotlin 默认的依赖是依赖了 kotlin-jre,而这个 jre 早就被改jetbrain废弃了,修改 jre 为 jdk 可以编辑这个文件。

Applications/Android\ Studio.app/Contents/plugins/android/lib/templates/activities/common/kotlin_macros.ftl  

下面是我自己使用的两个文件的内容,你可以直接复制走:

// simple.xml.ftl 文件的

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"/>

</LinearLayout>

// kotlin_macros.ftl 文件的

<#-- Macro used to add the necessary dependencies to support kotlin to
an app build.gradle -->

<#macro addKotlinPlugins>
<#if generateKotlin>
<#compress>
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
</#compress>
</#if>
</#macro>

<#macro addKotlinDependencies>
<#if generateKotlin>${getConfigurationName("compile")} "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"</#if>
</#macro>

// TODO: <apply plugin /> Is adding the dependencies at the *end* of build.gradle
// TODO: The two macros above, addKotlinPlugins and addKotlinDependencies, are duplicating the work of addAllKotlinDependencies, when
//       creating a new project (isNewProject == true). The only reason is the above bug on <apply plugin />
<#macro addAllKotlinDependencies>
  <#if (language!'Java')?string == 'Kotlin'>
    <#if !isNewProject>
      <apply plugin="kotlin-android" />
      <apply plugin="kotlin-android-extensions" />
    </#if>
    <#if !hasDependency('org.jetbrains.kotlin:kotlin-stdlib')>
        <dependency mavenUrl="org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"/>
        <merge from="root://activities/common/kotlin.gradle.ftl"
                 to="${escapeXmlAttribute(projectLocation)}/build.gradle" />
    </#if>
  </#if>
</#macro>
了解更多有深度技术的文章,与移动端、大前端未来方向的认知,前往订阅 开源实验室小专栏。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK