package com.example.qunzhang.testdemo;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
public class DimenTool {
public static void gen() {
//以此文件夹下的dimens.xml文件内容为初始值参照
File file = new File("./app/src/main/res/values/dimens.xml");
BufferedReader reader = null;
StringBuilder sw240 = new StringBuilder();
StringBuilder sw320 = new StringBuilder();
StringBuilder sw360 = new StringBuilder();
StringBuilder sw480 = new StringBuilder();
StringBuilder sw600 = new StringBuilder();
StringBuilder sw720 = new StringBuilder();
StringBuilder sw800 = new StringBuilder();
StringBuilder sw820 = new StringBuilder();
try {
System.out.println("生成不同分辨率:");
reader = new BufferedReader(new FileReader(file));
String tempString;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
if (tempString.contains("</dimen>")) {
//tempString = tempString.replaceAll(" ", "");
String start = tempString.substring(0, tempString.indexOf(">") + 1);
String end = tempString.substring(tempString.lastIndexOf("<") - 2);
//截取<dimen></dimen>标签内的内容,从>右括号开始,到左括号减2,取得配置的数字
Double num = Double.parseDouble
(tempString.substring(tempString.indexOf(">") + 1,
tempString.indexOf("</dimen>") - 2));
//根据不同的尺寸,计算新的值,拼接新的字符串,并且结尾处换行。
sw240.append(start).append(getNum(num * 0.75)).append(end).append("\r\n");
sw320.append(start).append(getNum(num * 1)).append(end).append("\r\n");
sw360.append(start).append(getNum(num * 1.125)).append(end).append("\r\n");
sw480.append(start).append(getNum(num * 1.5)).append(end).append("\r\n");
sw600.append(start).append(getNum(num * 1.875)).append(end).append("\r\n");
sw720.append(start).append(getNum(num * 2.25)).append(end).append("\r\n");
sw800.append(start).append(getNum(num * 2.5)).append(end).append("\r\n");
sw820.append(start).append(getNum(num * 2.562)).append(end).append("\r\n");
} else {
sw240.append(tempString).append("");
sw320.append(tempString).append("");
sw360.append(tempString).append("");
sw480.append(tempString).append("");
sw600.append(tempString).append("");
sw720.append(tempString).append("");
sw800.append(tempString).append("");
sw820.append(tempString).append("");
}
line++;
}
reader.close();
System.out.println("<!-- sw240 -->");
System.out.println(sw240);
System.out.println("<!-- sw320 -->");
System.out.println(sw320);
System.out.println("<!-- sw360 -->");
System.out.println(sw360);
System.out.println("<!-- sw480 -->");
System.out.println(sw480);
System.out.println("<!-- sw600 -->");
System.out.println(sw600);
System.out.println("<!-- sw720 -->");
System.out.println(sw720);
System.out.println("<!-- sw800 -->");
System.out.println(sw800);
String sw240file = "./app/src/main/res/values-sw240dp/dimens.xml";
String sw320file = "./app/src/main/res/values-sw320dp/dimens.xml";
String sw360file = "./app/src/main/res/values-sw360dp/dimens.xml";
String sw480file = "./app/src/main/res/values-sw480dp/dimens.xml";
String sw600file = "./app/src/main/res/values-sw600dp/dimens.xml";
String sw720file = "./app/src/main/res/values-sw720dp/dimens.xml";
String sw800file = "./app/src/main/res/values-sw800dp/dimens.xml";
String sw820file = "./app/src/main/res/values-sw820dp/dimens.xml";
//将新的内容,写入到指定的文件中去
writeFile(sw240file, sw240.toString());
writeFile(sw320file, sw320.toString());
writeFile(sw360file, sw360.toString());
writeFile(sw480file, sw480.toString());
writeFile(sw600file, sw600.toString());
writeFile(sw720file, sw720.toString());
writeFile(sw800file, sw800.toString());
writeFile(sw820file, sw820.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
private static double getNum(double num){
DecimalFormat df = new DecimalFormat("#####0.0");
return Double.parseDouble(df.format(num));
}
/**
* 写入方法
*/
public static void writeFile(String file, String text) {
PrintWriter out = null;
try {
out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
out.println(text);
} catch (IOException e) {
e.printStackTrace();
}
out.close();
}
public static void main(String[] args) {
gen();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android屏幕适配,自动生成不同的dimens.xml详解
共65个文件
xml:21个
png:10个
bin:9个
需积分: 49 21 下载量 189 浏览量
2019-01-08
17:21:27
上传
评论
收藏 1.02MB ZIP 举报
温馨提示
android中官方建议的屏幕适配方式,通过根据不同的分辨率在工程的res文件夹下自动建立不同的尺寸文件夹,每个文件夹下都自动生成dimens.xml文件。
资源推荐
资源详情
资源评论
收起资源包目录
TestDemo2.zip (65个子文件)
TestDemo2
.gitignore 147B
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 236B
local.properties 391B
build.gradle 573B
gradlew.bat 2KB
gradlew 5KB
TestDemo2.iml 886B
app
.gitignore 8B
build.gradle 1KB
proguard-rules.pro 772B
app.iml 12KB
libs
src
test
java
com
example
qunzhang
testdemo
ExampleUnitTest.java 406B
main
AndroidManifest.xml 743B
java
com
example
qunzhang
testdemo
DimenTool.java 6KB
MainActivity.java 1KB
DateUtils.java 3KB
res
values-sw320dp
dimens.xml 4KB
values
styles.xml 394B
colors.xml 214B
strings.xml 74B
dimens.xml 4KB
mipmap-xxhdpi
ic_launcher_round.png 10KB
ic_launcher.png 6KB
layout
activity_main.xml 2KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 276B
ic_launcher_round.xml 276B
mipmap-xhdpi
ic_launcher_round.png 7KB
ic_launcher.png 4KB
mipmap-hdpi
ic_launcher_round.png 5KB
ic_launcher.png 3KB
drawable
bg_btn_submit_enable_false.xml 155B
bg_btn_blue_selector.xml 756B
ic_launcher_background.xml 6KB
bg_btn_submit_enable.xml 155B
values-sw480dp
dimens.xml 4KB
values-sw600dp
dimens.xml 4KB
values-sw360dp
dimens.xml 4KB
mipmap-mdpi
ic_launcher_round.png 3KB
ic_launcher.png 2KB
values-sw720dp
dimens.xml 4KB
values-sw800dp
dimens.xml 4KB
values-sw240dp
dimens.xml 4KB
values-sw820dp
dimens.xml 4KB
mipmap-xxxhdpi
ic_launcher_round.png 15KB
ic_launcher.png 9KB
androidTest
java
com
example
qunzhang
testdemo
ExampleInstrumentedTest.java 768B
.gradle
4.4
fileChanges
last-build.bin 1B
taskHistory
taskHistory.bin 2.44MB
taskHistory.lock 17B
javaCompile
taskJars.bin 32KB
classAnalysis.bin 1023KB
taskHistory.bin 270KB
jarAnalysis.bin 420KB
javaCompile.lock 17B
fileContent
fileContent.lock 17B
fileHashes
fileHashes.lock 17B
resourceHashesCache.bin 24KB
fileHashes.bin 278KB
buildOutputCleanup
outputFiles.bin 21KB
cache.properties 51B
buildOutputCleanup.lock 17B
gradle.properties 739B
settings.gradle 16B
共 65 条
- 1
资源评论
游走的大千世界的烤腰子
- 粉丝: 523
- 资源: 23
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功