前言:android中常用的两种单位dp和sp,每个项目都会用到,自己一行一行的复制更改数字,太过繁琐,程序员哪能吃得了这苦
1.权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
6.0以上设备动态申请,我这里直接申请了,不做其他判断。。。。。
private static String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityCompat.requestPermissions(this, permissions, 1);
}
2.写入本地,因为android官方建议我们用偶数写布局所以,我这里是生成字体大小,生成dimens如法炮制
private void write() {
new Thread(new Runnable() {
@Override
public void run() {
String str = "";
for (int i = 0; i < 100; i++) {
if (i % 2 == 0)
str += "<dimen name=\"font_" + i + "\">" + i + "sp</dimen> \n";
}
try {
// data/data/com.****/file/font.txt
FileOutputStream fOut = openFileOutput("font.txt", MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(str);
osw.flush();
osw.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}).start();
}
3.最后结果如下,通过工具拷贝出来,大功告成