Android适配之dimens适配终极攻略(实际项目中应用方案)

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>

 

800x480效果
1280x720效果图
1280x768
1920x1080
2560x1440

 

1440x2880

 

看效果图,市面上绝大多数的手机尺寸都能够适配,可以看效果图,自己也可以试一下。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值