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>
看效果圖,市面上絕大多數的手機尺寸都能夠適配,可以看效果圖,自己也可以試一下。