您的位置:首页 > 移动开发 > Android开发

Android开发Tips(2)

2016-01-18 08:20 726 查看
欢迎Follow我的GitHub, 关注我的CSDN.

我会介绍关于Android的一些有趣的小知识点. 上一篇.



1. Dagger2的开发顺序

Module -> Component -> Application

首先模块(Module)创建需要提供的类实例, 其次把模块添加到组件(Component)中并提供需要注入的类, 最后把组件添加到应用(Application)中并提供接口.

// 模块
@Module
public class TestAppModule {
private final Context mContext;

public TestAppModule(Context context) {
mContext = context.getApplicationContext();
}

// 提供类实例
@AppScope
@Provides
public Context provideAppContext() {
return mContext;
}

@Provides
public WeatherApiClient provideWeatherApiClient() {
return new MockWeatherApiClient();
}
}

// 组件
@AppScope
@Component(modules = TestAppModule.class) // 注册模块
public interface TestAppComponent extends AppComponent {
void inject(MainActivityTest test);
}

// 应用
public class TestWeatherApplication extends WeatherApplication {
private TestAppComponent mTestAppComponent;

@Override public void onCreate() {
super.onCreate();
mTestAppComponent = DaggerTestAppComponent.builder()
.testAppModule(new TestAppModule(this))
.build();
}

// 提供组件
@Override
public TestAppComponent getAppComponent() {
return mTestAppComponent;
}
}


2. JRebel

Android调试工具, 不用编译, 就可以刷新一些项目修改. 不过功能已经被Android Studio 2.0 代替, 等待2.0正式发版.

3. 数据绑定(DataBinding)

DataBinding实现数据与页面的分离, 更符合面向对象的编程模式.

布局设置

<data>
<variable
name="weatherData"
type="clwang.chunyu.me.wcl_espresso_dagger_demo.data.WeatherData"/>
</data>

<TextView
android:id="@+id/temperature"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="@dimen/margin_large"
android:layout_marginTop="@dimen/margin_xlarge"
android:text="@{weatherData.temperatureCelsius}"
android:textAppearance="@style/TextAppearance.AppCompat.Display3"
tools:text="10°"/>


逻辑设置

private ActivityMainBinding mBinding; // 页面绑定类
mBinding = DataBindingUtil.setContentView(this, R.layout.activity_main); // 绑定页面
mBinding.weatherLayout.setVisibility(View.VISIBLE); // 使用Id
mBinding.setWeatherData(weatherData); // 绑定数据


4. ClassyShark

查看Apk信息的软件, 功能非常强大, 省去反编译的步骤, 主要功能:

(1) 在MultiDex中dex的详细信息.

(2) 使用NativeLibrary的详细信息.

(3) 类的详细信息.

(4) 数量统计.



5. CocoaPod安装

升级Mac系统, 可能会导致Pod命令消失, 需要重新安装Pod.

sudo gem install -n /usr/local/bin cocoapods


6. LaunchMode

LaunchMode包含四种模式,

(1) standard, 标准模式, 启动重新创建示例, 默认.

(2) singleTop, 栈顶复用模式, 位于栈顶, 启动不会被创建, 调用onNewIntent.

(3) singleTask, 栈内复用模式, 存在不会被创建, 调用onNewIntent.

(4) singleInstance, 单实例模式, 单独位于一个任务栈内, 复用.

7. TextView的标准字体

样式

style="@style/TextAppearance.AppCompat.Display4"
style="@style/TextAppearance.AppCompat.Display3"
style="@style/TextAppearance.AppCompat.Display2"
style="@style/TextAppearance.AppCompat.Display1"
style="@style/TextAppearance.AppCompat.Headline"
style="@style/TextAppearance.AppCompat.Title"
style="@style/TextAppearance.AppCompat.Subhead"
style="@style/TextAppearance.AppCompat.Body2"
style="@style/TextAppearance.AppCompat.Body1"
style="@style/TextAppearance.AppCompat.Caption"
style="@style/TextAppearance.AppCompat.Button"


显示



8. 自动生成DbHelper的脚本

下载地址

安装Jinja2.

pip install Jinja2


设置数据

CLASS Repo
String Id
String Name
String Description
String Owner
ENDCLASS


下载代码库. 生成代码.

python sql_lite_helper.py -f ~/Desktop/Repo -n SampleGenerate -p me.chunyu -a clwang


9. Gson的序列化参数

有些情况下, Json名称与变量不同, 需要指定.

@SerializedName("avatar_url") private String avatarUrl;


10. Proguard保留库

最简洁的方式是全部保留. 去除警告dontwarn, 保留类keep class.

# 在线更新
-dontwarn clwang.chunyu.me.**
-keep class clwang.chunyu.me.**{*;}


OK, That’s all! Enjoy It!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Mystra Android Tips