Shell批量修改app源码布局中组件的属性

这篇博客介绍了一种使用shell脚本批量修改Android应用布局文件中ImageView的scaleType属性的方法,从而实现快速统一更新。通过示例展示了如何使用脚本将scaleType从centerInside更改为fitXY,适用于需要批量更新相同属性的场景,提高了开发效率。

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

最近开发了一款应用,其显示页面有12个模板,并且适配了横竖屏,即对应了2*12个布局文件,现在有个需求就是修改这些模板中ImageView的缩放类型,于是写了一个shell,用于批量修改布局中相同的属性,有需要的小伙伴可以参考,shell脚本如下:

#!/bin/bash
proper=$1
oldvalue=$2
newvalue=$3
folder=$4
if [ ! ${folder} ] || [ ! -d $folder ] || [ ! ${proper} ] || [ ! ${oldvalue} ] || [ ! ${newvalue} ];then
   echo "usage:$0 proper oldValue newValue path"
   exit
fi
echo "proper = "$proper
echo "oldvalue = "$oldvalue
echo "newvalue = "$newvalue
files=`grep -r $proper --include=*.xml ${folder} | awk -F":" '{print $1}'`
sortfiles=()
for file in $files
do
  file_exist=false
  for sortf in ${sortfiles[@]}
  do
    #echo "sortf = "$sortf
    #echo "file = "$file
    if [ $file = $sortf ];then
      file_exist=true
      break
    fi
  done
  #echo "file_exist = "$file_exist
  if [ $file_exist = false ];then
    index=${#sortfiles[@]}
    #echo "index = "$index
    sortfiles[$index]=$file
    #echo $file
  fi
  #echo $file
done
for sfile in ${sortfiles[@]}
do
  echo "modify "$sfile
  sed -i "s#${proper}\s*=\s*\"${oldvalue}\"#${proper}=\"${newvalue}\"#g" $sfile
  #dos2unix $sfile
done

展示下效果,这里以修改ImageViewscaleType为例,将scaleTypecenterInside修改为fitXY

master@ubuntu-server:app$ ./updateXmlProper.sh "android:scaleType" "centerInside" "fitXY"
usage:./updateXmlProper.sh proper oldValue newValue path
master@ubuntu-server:app$ ./updateXmlProper.sh "android:scaleType" "centerInside" "fitXY" res/
proper = android:scaleType
oldvalue = centerInside
newvalue = fitXY
modify res/layout/info_display_layout_six.xml
modify res/layout/info_display_layout_seven.xml
modify res/layout/info_display_layout_four.xml
modify res/layout/info_display_layout_ten.xml
modify res/layout/info_display_layout_eight.xml
modify res/layout/info_display_layout_nine.xml
modify res/layout/info_display_layout_eleven.xml
modify res/layout/info_display_layout_one.xml
modify res/layout/info_display_layout_two.xml
modify res/layout/info_display_layout_twelve.xml
modify res/layout/info_display_layout_five.xml
modify res/layout/info_display_layout_three.xml
modify res/layout-land/info_display_layout_six.xml
modify res/layout-land/info_display_layout_seven.xml
modify res/layout-land/info_display_layout_four.xml
modify res/layout-land/info_display_layout_ten.xml
modify res/layout-land/info_display_layout_eight.xml
modify res/layout-land/info_display_layout_nine.xml
modify res/layout-land/info_display_layout_eleven.xml
modify res/layout-land/info_display_layout_one.xml
modify res/layout-land/info_display_layout_two.xml
modify res/layout-land/info_display_layout_twelve.xml
modify res/layout-land/info_display_layout_five.xml
modify res/layout-land/info_display_layout_three.xml
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值