How To Create Dimens Folder In Android Studio
This example for you to share the Android screen adapter tool class specific code, for your reference, the specific content is as follows
DimenTool
GitHub address
Android screen adaptation scheme, automatically generate different resolution values
The official screen adaptation method in Android is to create different size folders under the res folder of the project according to different resolutions, and create dimensions.xml file under each folder. Then calculate and configure different DP or SP units in the dimensions. XML folder according to different sizes. During the development, we found that Android screen adaptation needs to use a lot of sizes, and each size is set up with dimensions.xml. The numerical value in each file should be calculated in proportion, one by one with a calculator? It's too much trouble. Today, there is a good way to introduce it to you.
step
1. In the Java folder of the project, copy the project's dimensiontool. Java to this folder.
/** * Created by cdy on 2016/2/3. *Quickly generate the adapter class. If it fails to run directly, you need to put it into the directory file manually */ public class DimenTool { public static void gen() { //The contents of the dimensions.xml file in this folder are taken as the initial value reference File file = new File("./app/src/main/res/values/dimens.xml"); BufferedReader reader = null; StringBuilder sw240 = new StringBuilder(); StringBuilder sw320 = new StringBuilder(); StringBuilder sw360 = new StringBuilder(); StringBuilder sw480 = new StringBuilder(); StringBuilder sw600 = new StringBuilder(); StringBuilder sw720 = new StringBuilder(); StringBuilder sw800 = new StringBuilder(); StringBuilder w820 = new StringBuilder(); try { System. Out. Println ("generate different resolutions"); reader = new BufferedReader(new FileReader(file)); String tempString; int line = 1; //Read in one line at a time until null is the end of the file 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); //Intercept the content in the < dimension > < / dimension > tag, start with the > right bracket, and subtract 2 from the left bracket to obtain the configured number Double num = Double.parseDouble (tempString.substring(tempString.indexOf(">") + 1, tempString.indexOf("</dimen>") - 2)); //According to different sizes, calculate new values, splice new strings, and wrap at the end. sw240.append(start).append( num * 0.75).append(end).append("\r\n"); sw320.append(start).append( num * 1).append(end).append("\r\n"); sw360.append(start).append( num * 1.125).append(end).append("\r\n"); sw480.append(start).append(num * 1.5).append(end).append("\r\n"); sw600.append(start).append(num * 1.87).append(end).append("\r\n"); sw720.append(start).append(num * 2.25).append(end).append("\r\n"); sw800.append(start).append(num * 2.5).append(end).append("\r\n"); w820.append(start).append(num * 2.56).append(end).append("\r\n"); } else { sw240.append(tempString).append(""); sw320.append(tempString).append(""); sw360.append(tempString).append(""); sw480.append(tempString).append(""); sw600.append(tempString).append(""); sw720.append(tempString).append(""); sw800.append(tempString).append(""); w820.append(tempString).append(""); } line++; } reader.close(); System.out.println("<!-- sw240 -->"); System.out.println(sw240); System.out.println("<!-- sw320 -->"); System.out.println(sw320); System.out.println("<!-- sw360 -->"); System.out.println(sw360); 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 sw240file = "./app/src/main/res/values-sw240dp/dimens.xml"; String sw320file = "./app/src/main/res/values-sw320dp/dimens.xml"; String sw360file = "./app/src/main/res/values-sw360dp/dimens.xml"; String sw480file = "./app/src/main/res/values-sw480dp/dimens.xml"; String sw600file = "./app/src/main/res/values-sw600dp/dimens.xml"; String sw720file = "./app/src/main/res/values-sw720dp/dimens.xml"; String sw800file = "./app/src/main/res/values-sw800dp/dimens.xml"; String w820file = "./app/src/main/res/values-w820dp/dimens.xml"; //Write the new content to the specified file writeFile(sw240file, sw240.toString()); writeFile(sw320file, sw320.toString()); writeFile(sw360file, sw360.toString()); 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(); } } } } /** *Write method * */ 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(); } } 2. Write the values under the dimensions (0.5-360, which can be determined according to the project, mainly for the convenience of later use). You can directly copy the project dimensions into your project
3. Create folder (values-sw240dp, values-sw320dp, values-sw360dp… Create corresponding folder according to dimentool code)
4. Right click and run. Dimentools. Main to generate the corresponding dimensions in the values folder
The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.
How To Create Dimens Folder In Android Studio
Source: https://developpaper.com/android-screen-adaptation-tool-class-android-automatically-generates-values-of-different-resolutions/
Posted by: mannsdockly.blogspot.com

0 Response to "How To Create Dimens Folder In Android Studio"
Post a Comment