简体   繁体   中英

How to replace all dp value such as 50dp with a @dimen/50_dp at once?

I have got lots of XML files in which margin and padding dimensions are hardcode I have to replace all of them in format @denem/margin_20 ....etc在此处输入图片说明

I have made dimens and saved all the values I need. But I have lots of XML files it will take a lot of time for me to go and change one by one file manually在此处输入图片说明 SO I want to ask is there any trick to change all the values at once in Android Studio.

Thanks a lot for Helping.

Press Ctrl + Shift + R to replace and write "50dp" with inverted commas in find field . Then in replace field write @dimen/_50dp . Replace one occurrence and if it worked as expected and replace all other occurrences.

NOTE: if you want to change this in just res folder then you can select this folder only in find and replace dialog.

The approved answer is good but then you will have to do it one by one for each dimension so you can do the following:

  1. Press Ctrl + Shift + R to replace all.
  2. Select layout Directory by selecting "Directory" Option
  3. Select Regrex ".*" from right side of find text input
  4. in find first write (?!")\\d\\d(?=dp")
  5. in replace write @dimen/_$0
  6. Then click replace all
  7. do similar thing for (?!")\\d(?=dp")

It will replace all 2 digits and 1 digit dp values to dimension. You will manually need to edit negative and floating dp values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM