1、基于smallestWidth的适配方案,也就是sw-适配方案,大部分手机的宽度dp值集中在320-450之间,大部分1080P的手机应该都是360dp,390dp,411dp。可以在这个基础上,参考Android studio中的Virtual Device Configuration。
2、如何适配呢?
(1)创建 DimenTypes类
public enum DimenTypes {
//适配Android 3.2以上 大部分手机的sw值集中在 300-460之间
DP_sw__300(300), // values-sw300
DP_sw__310(310),
DP_sw__320(320),
DP_sw__330(330),
DP_sw__340(340),
DP_sw__350(350),
DP_sw__360(360),
DP_sw__370(370),
DP_sw__380(380),
DP_sw__390(390),
DP_sw__410(410),
DP_sw__420(420),
DP_sw__430(430),
DP_sw__440(440),
DP_sw__450(450),
DP_sw__460(460),
DP_sw__470(470),
DP_sw__480(480),
DP_sw__490(490),
DP_sw__400(400);
// 想生成多少自己以此类推
/**
* 屏幕最小宽度
*/
private int swWidthDp;
DimenTypes(int swWidthDp) {
this.swWidthDp = swWidthDp;
}
public int getSwWidthDp() {
return swWidthDp;
}
public void setSwWidthDp(int swWidthDp) {
this.swWidthDp = swWidthDp;
}
}
(2)创建自动生成xml文件的工具类,代码原理非常简单就是拼接xml。
import com.example.lib.constants.DimenTypes;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;
public class MakeUtils {
private static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
private static final String XML_RESOURCE_START = "<resources>\r\n";
private static final String XML_RESOURCE_END = "</resources>\r\n";
private static final String XML_DIMEN_TEMPLETE = "<dimen name=\"qb_%1$spx_%2$d\">%3$.2fdp</dimen>\r\n";
private static final String XML_BASE_DPI = "<dimen name=\"base_dpi\">%ddp</dimen>\r\n";
private static final int MAX_SIZE = 720;
/**
* 生成的文件名
*/
private static final String XML_NAME = "dimens.xml";
public static float px2dip(float pxValue, int sw,int designWidth) {
float dpValue = (pxValue/(float)designWidth) * sw;
BigDecimal bigDecimal = new BigDecimal(dpValue);
float finDp = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
return finDp;
}
/**
* 生成所有的尺寸数据
*
* @param type
* @return
*/
private static String makeAllDimens(DimenTypes type, int designWidth) {
float dpValue;
String temp;
StringBuilder sb = new StringBuilder();
try {
sb.append(XML_HEADER);
sb.append(XML_RESOURCE_START);
//备份生成的相关信息
temp = String.format(XML_BASE_DPI, type.getSwWidthDp());
sb.append(temp);
for (int i = 0; i <= MAX_SIZE; i++) {
dpValue = px2dip((float) i,type.getSwWidthDp(),designWidth);
temp = String.format(XML_DIMEN_TEMPLETE,"", i, dpValue);
sb.append(temp);
}
sb.append(XML_RESOURCE_END);
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
/**
* 生成的目标文件夹
* 只需传宽进来就行
*
* @param type 枚举类型
* @param buildDir 生成的目标文件夹
*/
public static void makeAll(int designWidth, DimenTypes type, String buildDir) {
try {
//生成规则
final String folderName;
if (type.getSwWidthDp() > 0) {
//适配Android 3.2+
folderName = "values-sw" + type.getSwWidthDp() + "dp";
}else {
return;
}
//生成目标目录
File file = new File(buildDir + File.separator + folderName);
if (!file.exists()) {
file.mkdirs();
}
//生成values文件
FileOutputStream fos = new FileOutputStream(file.getAbsolutePath() + File.separator + XML_NAME);
fos.write(makeAllDimens(type,designWidth).getBytes());
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、创建主函数入口文件,执行生成文件。
/**
* 设计稿尺寸(将自己设计师的设计稿的宽度填入)
*/
private static final int DESIGN_WIDTH = 1080;
/**
* 设计稿的高度 (将自己设计师的设计稿的高度填入)
*/
private static final int DESIGN_HEIGHT = 1920;
public static void main(String[] args) {
int smallest = DESIGN_WIDTH>DESIGN_HEIGHT? DESIGN_HEIGHT:DESIGN_WIDTH; // 求得最小宽度
DimenTypes[] values = DimenTypes.values();
for (DimenTypes value : values) {
File file = new File("");
MakeUtils.makeAll(smallest, value, file.getAbsolutePath());
}
}
(4)执行文件之后生成如下文件:
(5)把生成的文件夹拷入自己的文件夹中。
3、测试
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_50"
android:background="@color/colorAccent"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_100"
android:background="@color/colorPrimary"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_50"
android:background="@color/colorAccent"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_100"
android:background="@color/colorPrimary"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_50"
android:background="@color/colorAccent"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_100"
android:background="@color/colorPrimary"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
<TextView
android:layout_width="@dimen/qb_px_50"
android:layout_height="@dimen/qb_px_50"
android:background="@color/colorAccent"
android:text="你好"
android:textColor="@android:color/black"
android:textSize="18sp" />
</LinearLayout>
![](https://i-blog.csdnimg.cn/blog_migrate/a74c6a88a7f0add5c60078beec473319.png)
![](https://i-blog.csdnimg.cn/blog_migrate/df721dda643d3b54d5e254d8142b1278.png)
![](https://i-blog.csdnimg.cn/blog_migrate/407002731250aedd9e975a69fc8ad014.png)
![](https://i-blog.csdnimg.cn/blog_migrate/99fe3d1c0afe8748c2491f0a7aee9ac5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/9223080318ba33af8c06372fcda7f741.png)
![](https://i-blog.csdnimg.cn/blog_migrate/fb2eda8a48d63971907b697125bed3f3.png)
看效果图,市面上绝大多数的手机尺寸都能够适配,可以看效果图,自己也可以试一下。