dimens適配
最近團隊再做Android App的螢幕適配,按照官方的辦法是layout檔案引用dimen變數, 變數放在多個不同解析度下的dimens.xml檔案中,分別放在不同的目錄下,源地址::https://blog.csdn.net/offbye/article/details/48658097
- ./app/src/main/res/values-sw480dp-land/dimens.xml
- ./app/src/main/res/values-sw600dp-land/dimens.xml
- ./app/src/main/res/values-sw720dp-land/dimens.xml
- ./app/src/main/res/values-sw800dp-land/dimens.xml
- ./app/src/main/res/values-w820dp/dimens.xml
人工處理這些dimens.xml,感覺比較麻煩,就是個體力活,於是做了個工具類按比例自動化生成dimens.xml檔案,效率瞬間提高了很多倍,如果需要特殊定義尺寸,放到另一個xml檔案裡面就行了。
values/dimens.xml 檔案示例
- <resources>
- <!-- Default screen margins, per the Android Design guidelines. -->
- <dimen name="activity_horizontal_margin">14dp</dimen>
- <dimen name="activity_vertical_margin">14dp</dimen>
- <dimen name="action_bar_height">50dp</dimen>
- <dimen name="tab_height">81dp</dimen>
- <dimen name="content_margin">18dp</dimen>
- <dimen name="list_title_height">63dp</dimen>
- <dimen name="report_table_header_height">54dp</dimen>
- <dimen name="report_category_height">54dp</dimen>
- <dimen name="fab_size_normal">72dp</dimen>
- <dimen name="fab_margin">14dp</dimen>
工具類程式碼,直接執行即可
- 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;
- /**
- * 根據values/dimens.xml, 自動計算比例並生成不同解析度的dimens.xml
- * 注意用dp和sp,不要用dip,否則生成可能會出錯;xml值不要有空格
- * Created by zhangxitao on 15/9/22.
- */
- public class DimenTool {
- public static void gen() {
- File file = new File("./app/src/main/res/values/dimens.xml");
- BufferedReader reader = null;
- StringBuilder sw480 = new StringBuilder();
- StringBuilder sw600 = new StringBuilder();
- StringBuilder sw720 = new StringBuilder();
- StringBuilder sw800 = new StringBuilder();
- StringBuilder w820 = 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);
- int num = Integer.valueOf(tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2));
- sw480.append(start).append((int) Math.round(num * 0.6)).append(end).append("\n");
- sw600.append(start).append((int) Math.round(num * 0.75)).append(end).append("\n");
- sw720.append(start).append((int) Math.round(num * 0.9)).append(end).append("\n");
- sw800.append(tempString).append("\n");
- w820.append(tempString).append("\n");
- } else {
- sw480.append(tempString).append("\n");
- sw600.append(tempString).append("\n");
- sw720.append(tempString).append("\n");
- sw800.append(tempString).append("\n");
- w820.append(tempString).append("\n");
- }
- line++;
- }
- reader.close();
- 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 sw480file = "./app/src/main/res/values-sw480dp-land/dimens.xml";
- String sw600file = "./app/src/main/res/values-sw600dp-land/dimens.xml";
- String sw720file = "./app/src/main/res/values-sw720dp-land/dimens.xml";
- String sw800file = "./app/src/main/res/values-sw800dp-land/dimens.xml";
- String w820file = "./app/src/main/res/values-w820dp/dimens.xml";
- writeFile(sw480file, sw480.toString());
- writeFile(sw600file, sw600.toString());
- writeFile(sw720file, sw720.toString());
- writeFile(sw800file, sw800.toString());
- writeFile(w820file, w820.toString());
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (reader != null) {
- try {
- reader.close();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- }
- }
- 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適配: 拉伸適配的缺點Android
- flutter 螢幕尺寸適配 字型大小適配Flutter
- iphoneX適配iPhone
- Android適配Android
- Android螢幕適配(理論適配100%機型)Android
- 螢幕適配
- WebView iPhoneX適配WebViewiPhone
- iphoneX,XsMax適配iPhone
- 裝置適配
- 適配問題
- android螢幕適配三:通過畫素密度適配Android
- pc大屏適配
- Flutter螢幕適配Flutter
- Flutter深色模式適配Flutter模式
- 移動端適配
- rem 適配佈局REM
- h5適配H5
- Android圖示適配Android
- Rem 等比適配始末REM
- AutoLayout螢幕適配
- 關於字型適配
- android 螢幕適配Android
- android全面屏適配Android
- Android P 適配指南Android
- (原創)高DPI適配經驗系列:(四)高DPI適配示例
- Unity適配iPhone X---關於Home鍵指示器適配UnityiPhone
- 應用適配資料庫還是資料庫適配應用資料庫
- (原創)高DPI適配經驗系列:(二)按DPI範圍適配
- Flutter適配深色模式(DarkMode)Flutter模式
- Android中的icon適配Android
- viewport移動端適配View
- Android6.0~9.0適配Android
- Android Q 提前適配攻略Android
- Android 劉海屏適配Android
- 適配iOS11--contentInsetAdjustmentBehavioriOS
- rem移動端適配REM
- Android適配:DP簡述Android
- vw移動端適配