Android-------手机屏幕适配之文件适配

本文介绍了一个用于自动生成适配多种分辨率设备的XML布局文件的Java程序。该程序通过计算不同屏幕尺寸的比例,生成对应的dimen资源文件,以实现跨设备的UI一致性。适用于Android开发者进行多分辨率适配。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    public class Main {
        //定义文件本地存储路径,可按照需求更改
        private final static String rootPath = "D:\\workfiles\\layoutroot\\values-{0}x{1}";
    
        /** 横轴方向分为320份 */
        private final static float dw = 320f;
        /** 纵轴方向分为480份 */
        private final static float dh = 480f;
    
        /** 宽度的模板 */
        private final static String WTemplate = "<dimen name=\"x{0}\">{1}px</dimen>\n";
        /** 高度的模板 */
        private final static String HTemplate = "<dimen name=\"y{0}\">{1}px</dimen>\n";
    
        public static void main(String[] args) {
            //就适配了三个分辨率的手机屏幕
            makeString(320, 480);
            makeString(720, 1280);
            makeString(1080, 1920);
        }
        
        /**
         * 生成对应分辨率的dimen文件
         * 
         * @param width
         *            手机屏幕x方向的像素数量
         * @param height
         *            手机屏幕y方向的像素数量
         */
        private static void makeString(int width, int height) {
            StringBuffer sbWidth = new StringBuffer();
            sbWidth.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbWidth.append("<resources>");
            float cellw = width / dw;
            for (int i = 0; i < dw; i++) {
                sbWidth.append(WTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellw * i) + ""));
            }
            sbWidth.append(WTemplate.replace("{0}", "320").replace("{1}",
                    width + ""));
            sbWidth.append("</resources>");
    
            StringBuffer sbHeight = new StringBuffer();
            sbHeight.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n");
            sbHeight.append("<resources>");
            float cellh = height / dh;
            for (int i = 0; i < dh; i++) {
                sbHeight.append(HTemplate.replace("{0}", i + "").replace("{1}",
                        change(cellh * i) + ""));
            }
            sbHeight.append(HTemplate.replace("{0}", "480").replace("{1}",
                    height + ""));
            sbHeight.append("</resources>");
    
            String path = rootPath.replace("{0}", height + "").replace("{1}",
                    width + "");
            File rootFile = new File(path);
            if (!rootFile.exists()) {
                rootFile.mkdirs();
            }
    
            File layxFile = new File(path + "\\lay_x.xml");
            File layyFile = new File(path + "\\lay_y.xml");
    
            try {
                PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
                pw.print(sbWidth.toString());
                pw.close();
                pw = new PrintWriter(new FileOutputStream(layyFile));
                pw.print(sbHeight.toString());
                pw.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    
        /**
         * 将浮点数小数点后保留两位小数
         */
        private static float change(float a) {
            int temp = (int) (a * 100);
            return temp / 100f;
        }

    }

 

将生成的values文件复制到工程的res下,

使用用例:
android:layout_width="@dimen/x160"

转载于:https://www.cnblogs.com/tittles0k/p/5647735.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值