AndroidStudio修改修改Code Style

尽管AndroidStudio给我们提供了默认的代码风格够我们使用,但是有一些细节的地方并不是很好看,所以我决定自己修改一下自动排版。可在AndroidStudio的设置中修改代码风格,进入AS的设置找到”Code Style”选项,这里可以修改多种语言的代码风格,我们只关注”Java”即可

一、注解排版

修改前:

@BindView(R.id.tv_home_page)
TextView mTvHomePage;
@BindView(R.id.ll_home_page)
LinearLayout mLlHomePage;
@BindView(R.id.tv_listen)
TextView mTvListen;
@BindView(R.id.ll_listen)
LinearLayout mLlListen;
@BindView(R.id.iv_listen_util)
ImageView mIvListenUtil;
@BindView(R.id.tv_discover)
TextView mTvDiscover;

用过ButterKnife的同学都知道当页面的控件很多的时候会出现大量上述代码,非常的难看

修改后:

@BindView(R.id.tv_home_page) TextView mTvHomePage;
@BindView(R.id.ll_home_page) LinearLayout mLlHomePage;
@BindView(R.id.tv_listen) TextView mTvListen;
@BindView(R.id.ll_listen) LinearLayout mLlListen;
@BindView(R.id.iv_listen_util) ImageView mIvListenUtil;
@BindView(R.id.tv_discover) TextView mTvDiscover;
@BindView(R.id.ll_discover) LinearLayout mLlDiscover;
@BindView(R.id.tv_mine) TextView mTvMine;
@BindView(R.id.ll_mine) LinearLayout mLlMine;

节省了大量无用的换行使代码看上去更加清爽

具体修改见下图:
AndroidStudio修改修改Code Style_第1张图片

二、空方法体、空类等大括号不换行 ##

代码中偶尔会遇到空的方法体或者空类的情况,AS默认的风格会自动帮我们换行,这是没有必要的

修改前:

public void foo1(int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
}

Runnable r = () -> {
};

class TestInnerClass {
}

修改后:

public void foo1(int i1, int i2, int i3, int i4, int i5, int i6, int i7) {}

Runnable r = () -> {};

class TestInnerClass {}

修改详情:
AndroidStudio修改修改Code Style_第2张图片

三、见代码

修改前:

foo.test()
        .footestA()
        .bar("arg1",
                "arg2");

我就问你难不难受!难不难受!

修改后:

foo.test()
   .footestA()
   .bar("arg1",
        "arg2");

我就问你被治愈了没有!被治愈了没有!

修改详情:
AndroidStudio修改修改Code Style_第3张图片

最后

关于代码风格与排版都是个人的习惯问题,但是好的代码风格写出来的代码不光自己会看着舒服别人看着也会舒服,我相信没有哪个程序员会希望自己写的代码被别人说写得像屎一样吧

你可能感兴趣的:(安卓开发,code-style)