Android实例——全局修改dp/sp

全局修改dp/sp

可用于不同密度设备的适配,将xml文件放到指定目录,设置缩放因子

import os
import re

def adjust_values(content, factor):
    # 调整dp值和sp值
    content = re.sub(r'(\d+)(dp|sp)', lambda m: f"{int(float(m.group(1)) * factor)}{m.group(2)}", content)
    return content

def process_file(file_path, factor):
    try:
        with open(file_path, 'r', encoding='utf-8') as file:
            content = file.read()
        
        adjusted_content = adjust_values(content, factor)
        
        with open(file_path, 'w', encoding='utf-8') as file:
            file.write(adjusted_content)
        print(f"成功处理: {file_path}")
    except Exception as e:
        print(f"处理文件 {file_path} 时出错: {str(e)}")

def process_directory(directory_path, factor):
    for root, dirs, files in os.walk(directory_path):
        for file in files:
            if file.endswith('.xml'):
                file_path = os.path.join(root, file)
                process_file(file_path, factor)

# 使用示例
directory_path = "xml"  # XML文件所在目录的路径
adjustment_factor = 0.5  # 调整因子

process_directory(directory_path, adjustment_factor)

假如文件命名为adjust_xml_values.py,则在命令行运行

python adjust_xml_values.py
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值