第一步,先创建一个view信息的javabean类:
代码如下:
package com.zte.layout.adapter;
import android.view.View;
public class LayoutInformation
{
private View view;
private double viewWidth;
private double viewHeight;
private double viewMarginLeft;
private double viewMarginTop;
public static int R=-1;
public static int L=-2;
private int parentLayoutType;
public LayoutInformation(View view, double viewWidth, double viewHeight,
double viewMarginLeft, double viewMarginTop, int parentLayoutType)
{
this.view = view;
this.viewWidth = viewWidth;
this.viewHeight = viewHeight;
this.viewMarginLeft = viewMarginLeft;
this.viewMarginTop = viewMarginTop;
this.parentLayoutType=parentLayoutType;
}
public View getView()
{
return view;
}
public void setView(View view)
{
this.view = view;
}
public double getViewWidth()
{
return viewWidth;
}
public void setViewWidth(double viewWidth)
{
this.viewWidth = viewWidth;
}
public double getViewHeight()
{
return viewHeight;
}
public void setViewHeight(double viewHeight)
{
this.viewHeight = viewHeight;
}
public double getViewMarginLeft()
{
return viewMarginLeft;
}
public void setViewMarginLeft(double viewMarginLeft)
{
this.viewMarginLeft = viewMarginLeft;
}
public double getViewMarginTop()
{
return viewMarginTop;
}
public void setViewMarginTop(double viewMarginTop)
{
this.viewMarginTop = viewMarginTop;
}
public int getParentLayoutType()
{
return parentLayoutType;
}
public void setParentLayoutType(int parentLayoutType)
{
this.parentLayoutType = parentLayoutType;
}
}
第二步:创建一个计算方法:
代码如下:
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MyLayoutAdapter
{
public double STANDARD_SCREEN_WIDTH;
public double STANDARD_SCREEN_HEIGHT;
public double CURRENT_SCREEN_WIDTH;
public double CURRENT_SCREEN_HEIGHT;
public static final double STANDARD_DENSITY = 160;
private double CURRENT_DENSITY;
private double DENSITY_RATIO;
private double WIDTH_RATIO;
private double HEIGHT_RATIO;
private double viewStandardWidth;
private double viewStandardHeight;
private double viewStandardMarginLeft;
private double viewStandardMarginTop;
private double viewCurrentWidth;
private double viewCurrentHeight;
private double viewCurrentMarginLeft;
private double viewCurrentMarginTop;
private View view;
private int parentLayoutType;
private final int LAYOUT_TYPE_RELATiVELAYOUT = LayoutInformation.R;
private final int LAYOUT_TYPE_LINEARLAYOUT = LayoutInformation.L;
private final int LAYOUTPARAMS_WARP_CONTENT = LayoutParams.WRAP_CONTENT;
private final int LAYOUTPARAMS_FILL_PARENT = LayoutParams.FILL_PARENT;
private Context context;
public MyLayoutAdapter(Context context, double standardWidth,
double standardHeight)
{
this.context = context;
getScreenSize();
STANDARD_SCREEN_HEIGHT = standardHeight;
STANDARD_SCREEN_WIDTH = standardWidth;
// 计算宽高比率
WIDTH_RATIO = CURRENT_SCREEN_WIDTH / STANDARD_SCREEN_WIDTH;
HEIGHT_RATIO = CURRENT_SCREEN_HEIGHT / STANDARD_SCREEN_HEIGHT;
}
private void getScreenSize()
{
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay()
getMetrics(displayMetrics);
CURRENT_SCREEN_WIDTH = displayMetrics.widthPixels;
CURRENT_SCREEN_HEIGHT = displayMetrics.heightPixels;
CURRENT_DENSITY = displayMetrics.densityDpi;
DENSITY_RATIO = STANDARD_DENSITY / CURRENT_DENSITY;
}
public void setVi ewLayout(List<LayoutInformation> listdata)
{
for (int i = 0; i < listdata.size(); i++)
{
view = listdata.get(i).getView();
viewStandardWidth = listdata.get(i).getViewWidth();
viewStandardHeight = listdata.get(i).getViewHeight();
viewStandardMarginLeft = listdata.get(i).getViewMarginLeft();
viewStandardMarginTop = listdata.get(i).getViewMarginTop();
setLayoutParams();
viewCurrentMarginLeft = viewStandardMarginLeft * WIDTH_RATIO;
viewCurrentMarginTop = viewStandardMarginTop * HEIGHT_RATIO;
parentLayoutType = listdata.get(i).getParentLayoutType();
setLayoutByParentLayoutType();
}
}
private void setLayoutParams()
{
// 如果基准的宽是wrap_content或者fill_parent则使用原值,否则进行计算得到通配后的值
if (viewStandardWidth == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardWidth == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentWidth = viewStandardWidth;
} else
{
viewCurrentWidth = viewStandardWidth * WIDTH_RATIO;
}
// 如果基准的宽是wrap_content或者fill_parent则使用原值,否则进行计算得到通配后的值
if (viewStandardHeight == LAYOUTPARAMS_WARP_CONTENT
|| viewStandardHeight == LAYOUTPARAMS_FILL_PARENT)
{
viewCurrentHeight = viewStandardHeight;
} else
{
viewCurrentHeight = viewStandardHeight * HEIGHT_RATIO;
}
}
private void setLayoutByParentLayoutType()
{
if (parentLayoutType == LAYOUT_TYPE_RELATiVELAYOUT)
{
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
(int) viewCurrentWidth, (int) viewCurrentHeight);
params.setMargins((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
view.setLayoutParams(params);
} else if (parentLayoutType == LAYOUT_TYPE_LINEARLAYOUT)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
(int) viewCurrentWidth, (int) viewCurrentHeight);
params.setMargins((int) viewCurrentMarginLeft,
(int) viewCurrentMarginTop, 0, 0);
view.setLayoutParams(params);
}
}
public int setTextSize(int standardSize)
{
int currentSize;
currentSize = (int) (standardSize * WIDTH_RATIO * DENSITY_RATIO);
return currentSize;
}
}
第三步,写一个接口:
代码如下:
public interface InitAllView{
public void initAllView();
}
第四步:代码控制:
代码如下:
private void initWildcard() {
myLayout = new MyLayoutAdapter(this, 320, 480);
listInfo = new ArrayList<LayoutInformation>();
listInfo.add(new LayoutInformation(mBtn1, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT, 0, 0, LayoutInformation.R));
listInfo.add(new LayoutInformation(mNowRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation.R));
listInfo.add(new LayoutInformation(mNextRegisterBtn, 80, 27.3, 14.7, 0,
LayoutInformation.R));
// listInfo.add(new LayoutInformation(mCheckBtn, 17.3,17.3, 14.7, 0,
// LayoutInformation.L));
mBtn1.setTextSize(myLayout.setTextSize(12));
mNowRegisterBtn.setTextSize(myLayout.setTextSize(12));
mNextRegisterBtn.setTextSize(myLayout.setTextSize(12));
myLayout.setViewLayout(listInfo);
}
您可能感兴趣的文章:Android 8.0系统中通知栏的适配详解Android 8.0系统中通知栏的适配微技巧Android 8.0系统中应用图标的适配微技巧Android-屏幕适配需要注意的地方总结Android的分辨率和屏幕适配详解Android 悬浮窗权限各机型各系统适配大全(总结)Android适配安卓6.0蓝牙通讯实现过程适配Android 8.0版本更新安装与通知栏的一些坑