屏幕适配+国际化

一、屏幕适配

1、创建一个MakeXml类(主要是为了生成各个手机屏幕的同一单位,单一运行这一个页面,让他自动生成文件,在把文件放在项目中)

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MakeXml {
    private final static String rootPath = "C:\\Users\\DELL-\\Desktop\\layoutroot\\values-{0}x{1}\\";
    private final static float dw = 1080f;
    private final static float dh = 1920f;
    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(480,800);
        makeString(480, 854);
        makeString(540, 960);
        makeString(600, 1024);
        makeString(720, 1184);
        makeString(720, 1196);
        makeString(720, 1280);
        makeString(768, 1024);
        makeString(800, 1280);
        makeString(1080, 1812);
        makeString(1080, 1920);
        makeString(1440, 2560);
    }
    public static void makeString(int w, int h) {
        StringBuffer sb = new StringBuffer();
        sb.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n");
        sb.append("[resources]");
        float cellw = w / dw;
        for (int i = 1; i < 320; i++) {
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellw * i) + ""));
        }
        sb.append(WTemplate.replace("{0}", "320").replace("{1}", w + ""));
        sb.append("[/resources]");
        StringBuffer sb2 = new StringBuffer();
        sb2.append("[?xml version=\"1.0\" encoding=\"utf-8\"?]\n");
        sb2.append("[resources]");
        float cellh = h / dh;
        for (int i = 1; i < 480; i++) {
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellh * i) + ""));
        }
        sb2.append(HTemplate.replace("{0}", "480").replace("{1}", h + ""));
        sb2.append("[/resources]");
        String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
        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(sb.toString());
            pw.close();
            pw = new PrintWriter(new FileOutputStream(layyFile));
            pw.print(sb2.toString());
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    public static float change(float a) {
        int temp = (int) (a * 100);
        return temp / 100f;
    }
}
 
 

如图:

生成的文件保存到res目录下并把名字改为 values-xxhdpi(最近手机大部分都是这个值 1920*1080)

2、写布局(开始适配)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="wangyanyong.packag.com.pingmu_shipei.MainActivity">
    <LinearLayout
        android:background="#03A9F4"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="@dimen/y45">
        <ImageView
            android:layout_marginLeft="@dimen/x7"
            android:src="@drawable/touxiang"
            android:layout_width="@dimen/x41"
            android:layout_height="@dimen/y41" />
        <TextView
            android:textColor="#ffffff"
            android:text="@string/title_name"
            android:textSize="16dp"
            android:layout_marginLeft="@dimen/x123"
            android:layout_width="@dimen/x33"
            android:layout_height="@dimen/y45" />
        <ImageView
            android:layout_marginLeft="@dimen/x130"
            android:src="@drawable/touxiang"
            android:layout_width="@dimen/x41"
            android:layout_height="@dimen/y45" />
    </LinearLayout>
    <ImageView
        android:src="@drawable/ad1"
        android:layout_width="match_parent"
        android:layout_height="@dimen/y138" />
</LinearLayout>
宽用@dimen/x45   设置(后面的45指的是美工标注的px值),高用@dimen/x45  (和宽一样)
适配完成


二、国际化

1、右键res目录 ——> new ——>Android resource directoy




它会弹出一个框 ,如上图

选择Locale 再点击 右边的  “ >> ” 按钮 






再选择 国家 、地区




如上图,我选择的是 美国  , 所有地区

之后点击 OK 

它会自动创建一个 values-en 文件夹  如图:



再把values文件夹下的string 粘贴到 values-en 中

国际化也就完成了。。。。。




猜你喜欢

转载自blog.csdn.net/qq_40089806/article/details/80023229