根据Android Layout XM文件自动生成自定义View代码的工具类
工具代码:
package com.xiaolei;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Attr;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
/**
* @author xiaolei
*/
public class Main
{
static Map<String, Integer> fieldNameMap = new HashMap<String, Integer>();
private static boolean UseDensityUtil = false;
public static StringBuilder builder = new StringBuilder();
public static Set<String> importList = new HashSet<String>();
public static void main(String[] args) throws Exception
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File("E:\\xiaolei\\EclipseProjects\\测试单点登录\\res\\layout\\test2.xml"));
// 获得根元素结点
Element root = doc.getDocumentElement();
parseRootElement(root);
builder.append("\n\t}");
if(UseDensityUtil)
{
writeDp2PX();
}
builder.append("\n}");
// 导包
for(String im : importList)
{
builder.insert(0, im+"\n");
}
System.out.println(builder);
}
public static void parseElement(String parent,Element element)
{
NodeList children = element.getChildNodes();
String fildName = getAttr(element);
builder.append("\n\t\t"+parent+".addView("+fildName+");");
// System.out.print(">");
for (int i = 0; i < children.getLength(); i++)
{
Node node = children.item(i);
// 获得结点的类型
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE)
{
// 是元素,继续递归
parseElement(fildName,(Element) node);
} else if (nodeType == Node.TEXT_NODE)
{
// 递归出口
builder.append("\n");
} else if (nodeType == Node.COMMENT_NODE)
{
builder.append("//--");
Comment comment = (Comment) node;
// 注释内容
String data = comment.getData();
builder.append(data);
}
}
}
public static void parseRootElement(Element element)
{
NodeList children = element.getChildNodes();
getRootAttr(element);
// System.out.print(">");
for (int i = 0; i < children.getLength(); i++)
{
Node node = children.item(i);
// 获得结点的类型
short nodeType = node.getNodeType();
if (nodeType == Node.ELEMENT_NODE)
{
// 是元素,继续递归
parseElement("this",(Element) node);
} else if (nodeType == Node.TEXT_NODE)
{
// 递归出口
builder.append("\n");
} else if (nodeType == Node.COMMENT_NODE)
{
builder.append("//--");
Comment comment = (Comment) node;
// 注释内容
String data = comment.getData();
builder.append(data);
}
}
}
public static String getAttr(Element element)
{
String tagName = element.getNodeName();
String fildName = getFieldName(tagName);
builder.append("\n\t\t" + tagName + " " + fildName + " = new " + tagName + "(context);");
// element元素的所有属性所构成的NamedNodeMap对象,需要对其进行判断
Map<String, String> atts = new HashMap<String, String>();
NamedNodeMap map = element.getAttributes();
// 如果该元素存在属性
if (null != map)
{
for (int i = 0; i < map.getLength(); i++)
{
// 获得该元素的每一个属性
Attr attr = (Attr) map.item(i);
String attrName = attr.getName();
String attrValue = attr.getValue();
atts.put(attrName, attrValue);
}
compile(tagName, fildName, atts);
}
return fildName;
}
public static void getRootAttr(Element element)
{
String tagName = element.getNodeName();
String fildName = "this";
builder.insert(0, "import android.content.Context;\n");
builder.insert(0, "import android.util.AttributeSet;\n");
builder.append("\npublic class mView extends " + tagName);
builder.append("\n{");
builder.append("\n public mView(Context context)");
builder.append("\n {");
builder.append("\n this(context,null);");
builder.append("\n }");
builder.append("\n public mView(Context context, AttributeSet attrs)");
builder.append("\n {");
builder.append("\n this(context, attrs,0);");
builder.append("\n }");
builder.append("\n public mView(Context context, AttributeSet attrs, int defStyle)");
builder.append("\n {");
builder.append("\n super(context, attrs, defStyle);");
builder.append("\n InitUI(context);");
builder.append("\n }");
builder.append("\n public void InitUI(Context context)");
builder.append("\n {");
// element元素的所有属性所构成的NamedNodeMap对象,需要对其进行判断
Map<String, String> atts = new HashMap<String, String>();
NamedNodeMap map = element.getAttributes();
// 如果该元素存在属性
if (null != map)
{
for (int i = 0; i < map.getLength(); i++)
{
// 获得该元素的每一个属性
Attr attr = (Attr) map.item(i);
String attrName = attr.getName();
String attrValue = attr.getValue();
atts.put(attrName, attrValue);
}
compile(tagName, fildName, atts);
}
}
/**
* 为控件取名字
*
* @param type
* 控件类型
* @return
*/
public static String getFieldName(String type)
{
Integer num = fieldNameMap.get(type);
if (num == null)
{
num = 1;
} else
{
num++;
}
fieldNameMap.put(type, num);
String s[] = type.split("\\.");
return (s[s.length - 1] + num).toLowerCase();
}
/**
* 根据属性 名称 和类型 自动生成相应代码
*
* @param type
* @param fieldname
* @param atts
*/
public static void compile(String type, String fieldname, Map<String, String> atts)
{
String height = atts.get("android:layout_height").toUpperCase();
String width = atts.get("android:layout_width").toUpperCase();
String weight = atts.get("android:layout_weight");
String params = getFieldName("LayoutParams");
if(height.toUpperCase().endsWith("DP")||height.toUpperCase().endsWith("DIP")||height.toUpperCase().endsWith("PX"))
{
height = getNumFromDemin(height);
height = "DensityUtil.dip2px(context, "+height+")";
}else
{
height = "LayoutParams."+ height;
}
if(width.toUpperCase().endsWith("DP")||width.toUpperCase().endsWith("DIP")||width.toUpperCase().endsWith("PX"))
{
width = getNumFromDemin(width);
width = "DensityUtil.dip2px(context, "+width+")";
}else
{
width = "LayoutParams."+ width;
}
if (weight == null)
{
builder.append("\n\t\tLayoutParams " + params + " = new LayoutParams(" + width+ ", " + height + ");");
} else
{
builder.append("\n\t\tLayoutParams " + params + " = new LayoutParams("+ width+ "," + height + "," + weight + ");");
}
String layout_gravity = atts.get("android:layout_gravity");
if (layout_gravity != null)
{
importList.add("import android.view.Gravity;");
builder.append("\n\t\t" + params + ".gravity = Gravity." + layout_gravity.toUpperCase() + ";");
}
String layout_margin = atts.get("android:layout_margin");
if (layout_margin != null)
{
layout_margin = getNumFromDemin(layout_margin);
builder.append("\n\t\t" + params + ".setMargins(DensityUtil.dip2px(context, " + layout_margin
+ "), DensityUtil.dip2px(context, " + layout_margin + "), DensityUtil.dip2px(context, "
+ layout_margin + "), DensityUtil.dip2px(context, " + layout_margin + "));");
}
String layout_left = "0";
String layout_top = "0";
String layout_right = "0";
String layout_bottom = "0";
String layout_marginLeft = atts.get("android:layout_marginLeft");
if (layout_marginLeft != null)
{
layout_marginLeft = getNumFromDemin(layout_marginLeft);
layout_left = layout_marginLeft;
}
String layout_marginStart = atts.get("android:layout_marginStart");
if (layout_marginStart != null)
{
layout_marginStart = getNumFromDemin(layout_marginStart);
layout_left = layout_marginStart;
}
String layout_marginTop = atts.get("android:layout_marginTop");
if (layout_marginTop != null)
{
layout_marginTop = getNumFromDemin(layout_marginTop);
layout_top = layout_marginTop;
}
String layout_marginBottom = atts.get("android:layout_marginBottom");
if (layout_marginBottom != null)
{
layout_marginBottom = getNumFromDemin(layout_marginBottom);
layout_bottom = layout_marginBottom;
}
String layout_marginRight = atts.get("android:layout_marginRight");
if (layout_marginRight != null)
{
layout_marginRight = getNumFromDemin(layout_marginRight);
layout_right = layout_marginRight;
}
String layout_marginEnd = atts.get("android:layout_marginEnd");
if (layout_marginEnd != null)
{
layout_marginEnd = getNumFromDemin(layout_marginEnd);
layout_right = layout_marginEnd;
}
if(!(layout_left.equals("0") && layout_top.equals("0") && layout_right.equals("0") && layout_bottom.equals("0")))
{
builder.append("\n\t\t" + params + ".setMargins(DensityUtil.dip2px(context, " + layout_left
+ "), DensityUtil.dip2px(context, " + layout_top + "), DensityUtil.dip2px(context, " + layout_right
+ "), DensityUtil.dip2px(context, " + layout_bottom + "));");
}
builder.append("\n\t\t" + fieldname + ".setLayoutParams(" + params + ");");
String orientation = atts.get("android:orientation");
if(orientation != null)
{
importList.add("import android.widget.LinearLayout;");
builder.append("\n\t\t" + fieldname + ".setOrientation(LinearLayout." + orientation.toUpperCase() + ");");
}
String visible = atts.get("android:visibility");
if (visible != null)
{
importList.add("import android.view.View;");
builder.append("\n\t\t" + fieldname + ".setVisibility(View." + visible.toUpperCase() + ");");
}
String text = atts.get("android:text");
if (text != null)
{
builder.append("\n\t\t" + fieldname + ".setText(\"" + text + "\");");
}
String hint = atts.get("android:hint");
if (hint != null)
{
builder.append("\n\t\t" + fieldname + ".setHint(\"" + hint + "\");");
}
String ems = atts.get("android:ems");
if (ems != null)
{
builder.append("\n\t\t" + fieldname + ".setEms(" + ems + ");");
}
String gravity = atts.get("android:gravity");
if (gravity != null)
{
importList.add("import android.view.Gravity;");
builder.append("\n\t\t" + fieldname + ".setGravity(Gravity." + gravity.toUpperCase() + ");");
}
String textSize = atts.get("android:textSize");
if (textSize != null)
{
textSize = getNumFromDemin(textSize);
builder.append("\n\t\t" + fieldname + ".setTextSize(DensityUtil.sp2px(context, " + textSize + "));");
}
String padding = atts.get("android:padding");
if(padding != null)
{
padding = getNumFromDemin(padding);
builder.append("\n\t\t" + fieldname + ".setPadding(DensityUtil.dip2px(context, " + padding + "),DensityUtil.dip2px(context, " + padding + "),DensityUtil.dip2px(context, " + padding + "),DensityUtil.dip2px(context, " + padding + "));");
}
String android_paddingLeft = "0";
String android_paddingTop = "0";
String android_paddingRight = "0";
String android_paddingBottom = "0";
String paddingLeft = atts.get("android:paddingLeft");
if(paddingLeft != null)
{
paddingLeft = getNumFromDemin(paddingLeft);
android_paddingLeft = paddingLeft;
}
String paddingStart = atts.get("android:paddingStart");
if(paddingStart != null)
{
paddingStart = getNumFromDemin(paddingStart);
android_paddingLeft = paddingStart;
}
String paddingTop = atts.get("android:paddingTop");
if(paddingTop != null)
{
paddingTop = getNumFromDemin(paddingTop);
android_paddingTop = paddingTop;
}
String paddingRight = atts.get("android:paddingRight");
if(paddingRight != null)
{
paddingRight = getNumFromDemin(paddingRight);
android_paddingRight = paddingRight;
}
String paddingEnd = atts.get("android:paddingEnd");
if(paddingEnd != null)
{
paddingEnd = getNumFromDemin(paddingEnd);
android_paddingRight = paddingEnd;
}
String paddingBottom = atts.get("android:paddingBottom");
if(paddingBottom != null)
{
paddingBottom = getNumFromDemin(paddingBottom);
android_paddingBottom = paddingBottom;
}
if(!(android_paddingLeft.equals("0") && android_paddingRight.equals("0") && android_paddingTop.equals("0") && android_paddingBottom.equals("0")))
{
builder.append("\n\t\t" + fieldname + ".setPadding(DensityUtil.dip2px(context, " + android_paddingLeft + "),DensityUtil.dip2px(context, " + android_paddingTop + "),DensityUtil.dip2px(context, " + android_paddingRight + "),DensityUtil.dip2px(context, " + android_paddingBottom + "));");
}
String textColor = atts.get("android:textColor");
if(textColor != null)
{
importList.add("import android.graphics.Color;");
builder.append("\n\t\t" + fieldname + ".setTextColor(Color.parseColor(\""+textColor+"\"));");
}
String numColumns = atts.get("android:numColumns");
if(numColumns != null)
{
builder.append("\n\t\t" + fieldname + ".setNumColumns("+numColumns+");");
}
String scrollbars = atts.get("android:scrollbars");
if(scrollbars != null)
{
if("none".equals(scrollbars))
{
builder.append("\n\t\t" + fieldname + ".setHorizontalScrollBarEnabled(false);");
builder.append("\n\t\t" + fieldname + ".setVerticalScrollBarEnabled(false);");
}else if ("horizontal".equals(scrollbars))
{
builder.append("\n\t\t" + fieldname + ".setHorizontalScrollBarEnabled(true);");
}else if ("vertical".equals(scrollbars))
{
builder.append("\n\t\t" + fieldname + ".setVerticalScrollBarEnabled(true);");
}
}
String alpha = atts.get("android:alpha");
if(alpha != null)
{
builder.append("\n\t\t" + fieldname + ".setAlpha("+alpha+");");
}
String contentDescription = atts.get("android:contentDescription");
if(contentDescription != null)
{
builder.append("\n\t\t" + fieldname + ".setContentDescription("+contentDescription+");");
}
String fadeScrollbars = atts.get("android:fadeScrollbars");
if(fadeScrollbars!=null)
{
builder.append("\n\t\t" + fieldname + ".setScrollbarFadingEnabled("+fadeScrollbars+");");
}
String focusable = atts.get("android:focusable");
if(focusable!=null)
{
builder.append("\n\t\t" + fieldname + ".setFocusable("+focusable+");");
}
String focusableInTouchMode = atts.get("android:focusableInTouchMode");
if(focusableInTouchMode!=null)
{
builder.append("\n\t\t" + fieldname + ".setFocusableInTouchMode("+focusableInTouchMode+");");
}
String minHeight = atts.get("android:minHeight");
if(minHeight!=null)
{
minHeight = getNumFromDemin(minHeight);
builder.append("\n\t\t" + fieldname + ".setMinimumHeight(DensityUtil.dip2px(context,"+minHeight+"));");
}
String minWidth = atts.get("android:minWidth");
if(minWidth != null)
{
minWidth = getNumFromDemin(minWidth);
builder.append("\n\t\t" + fieldname + ".setMinimumWidth(DensityUtil.dip2px(context,"+minWidth+"));");
}
String tag = atts.get("android:tag");
if(tag != null)
{
builder.append("\n\t\t" + fieldname + ".setTag(\""+tag+"\");");
}
}
public static String getNumFromDemin(String demin)
{
UseDensityUtil = true;
String result = "";
if(demin.toLowerCase().endsWith("dp") || demin.toLowerCase().endsWith("px") || demin.toLowerCase().endsWith("sp"))
{
result = demin.substring(0, demin.length() - 2);
}else if(demin.toLowerCase().endsWith("dip"))
{
result = demin.substring(0, demin.length() - 3);
}else {
result = demin;
}
return result;
}
public static void writeDp2PX()
{
builder.append("\n\t/**");
builder.append("\n\t * dp、sp 转换为 px 的工具类");
builder.append("\n\t * ");
builder.append("\n\t */");
builder.append("\n\tpublic static class DensityUtil {");
builder.append("\n\t /**");
builder.append("\n\t * 将px值转换为dip或dp值,保证尺寸大小不变");
builder.append("\n\t * @param pxValue");
builder.append("\n\t * @param scale (DisplayMetrics类中属性density)");
builder.append("\n\t * @return");
builder.append("\n\t */");
builder.append("\n\t public static int px2dip(Context context, float pxValue)");
builder.append("\n\t {");
builder.append("\n\t final float scale = context.getResources().getDisplayMetrics().density;");
builder.append("\n\t return (int) (pxValue / scale + 0.5f);");
builder.append("\n\t }");
builder.append("\n\t");
builder.append("\n\t /**");
builder.append("\n\t * 将dip或dp值转换为px值,保证尺寸大小不变");
builder.append("\n\t * @param dipValue");
builder.append("\n\t * @param scale(DisplayMetrics类中属性density)");
builder.append("\n\t * @return ");
builder.append("\n\t */");
builder.append("\n\t public static int dip2px(Context context, float dipValue)");
builder.append("\n\t {");
builder.append("\n\t final float scale = context.getResources().getDisplayMetrics().density;");
builder.append("\n\t return (int) (dipValue * scale + 0.5f);");
builder.append("\n\t }");
builder.append("\n\t");
builder.append("\n\t /**");
builder.append("\n\t * 将px值转换为sp值,保证文字大小不变");
builder.append("\n\t * @param pxValue");
builder.append("\n\t * @param fontScale DisplayMetrics类中属性scaledDensity");
builder.append("\n\t * @return");
builder.append("\n\t */");
builder.append("\n\t public static int px2sp(Context context, float pxValue)");
builder.append("\n\t {");
builder.append("\n\t final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;");
builder.append("\n\t return (int) (pxValue / fontScale + 0.5f);");
builder.append("\n\t }");
builder.append("\n\t");
builder.append("\n\t /**");
builder.append("\n\t * 将sp值转换为px值,保证文字大小不变");
builder.append("\n\t *");
builder.append("\n\t * @param spValue");
builder.append("\n\t * @param fontScale (DisplayMetrics类中属性scaledDensity)");
builder.append("\n\t * @return");
builder.append("\n\t */");
builder.append("\n\t public static int sp2px(Context context, float spValue)");
builder.append("\n\t {");
builder.append("\n\t final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;");
builder.append("\n\t return (int) (spValue * fontScale + 0.5f);");
builder.append("\n\t }");
builder.append("\n\t}");
}
}
看效果:
这是生成的自定义View的Java文件:
import android.view.View;
import android.widget.LinearLayout;
import android.view.Gravity;
import android.graphics.Color;
import android.util.AttributeSet;
import android.content.Context;
public class mView extends LinearLayout
{
public mView(Context context)
{
this(context,null);
}
public mView(Context context, AttributeSet attrs)
{
this(context, attrs,0);
}
public mView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
InitUI(context);
}
public void InitUI(Context context)
{
LayoutParams layoutparams1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
this.setLayoutParams(layoutparams1);
this.setOrientation(LinearLayout.VERTICAL);
FrameLayout framelayout1 = new FrameLayout(context);
LayoutParams layoutparams2 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
framelayout1.setLayoutParams(layoutparams2);
this.addView(framelayout1);
LinearLayout linearlayout1 = new LinearLayout(context);
LayoutParams layoutparams3 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearlayout1.setLayoutParams(layoutparams3);
linearlayout1.setOrientation(LinearLayout.VERTICAL);
linearlayout1.setVisibility(View.VISIBLE);
framelayout1.addView(linearlayout1);
LinearLayout linearlayout2 = new LinearLayout(context);
LayoutParams layoutparams4 = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 48));
linearlayout2.setLayoutParams(layoutparams4);
linearlayout2.setOrientation(LinearLayout.HORIZONTAL);
linearlayout2.setGravity(Gravity.CENTER_VERTICAL);
linearlayout1.addView(linearlayout2);
TextView textview1 = new TextView(context);
LayoutParams layoutparams5 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview1.setLayoutParams(layoutparams5);
textview1.setText("←");
textview1.setGravity(Gravity.CENTER_VERTICAL);
textview1.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
linearlayout2.addView(textview1);
LinearLayout linearlayout3 = new LinearLayout(context);
LayoutParams layoutparams6 = new LayoutParams(DensityUtil.dip2px(context, 0),LayoutParams.MATCH_PARENT,1);
linearlayout3.setLayoutParams(layoutparams6);
linearlayout3.setOrientation(LinearLayout.HORIZONTAL);
linearlayout3.setGravity(Gravity.CENTER);
linearlayout2.addView(linearlayout3);
TextView textview2 = new TextView(context);
LayoutParams layoutparams7 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview2.setLayoutParams(layoutparams7);
textview2.setText("九月");
textview2.setGravity(Gravity.CENTER_VERTICAL);
textview2.setPadding(DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0));
linearlayout3.addView(textview2);
TextView textview3 = new TextView(context);
LayoutParams layoutparams8 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview3.setLayoutParams(layoutparams8);
textview3.setText("1994");
textview3.setGravity(Gravity.CENTER_VERTICAL);
textview3.setPadding(DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0));
linearlayout3.addView(textview3);
TextView textview4 = new TextView(context);
LayoutParams layoutparams9 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
layoutparams9.setMargins(DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
textview4.setLayoutParams(layoutparams9);
textview4.setText("→");
textview4.setGravity(Gravity.CENTER_VERTICAL);
textview4.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
linearlayout2.addView(textview4);
GridView gridview1 = new GridView(context);
LayoutParams layoutparams10 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutparams10.setMargins(DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
gridview1.setLayoutParams(layoutparams10);
gridview1.setPadding(DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0));
gridview1.setNumColumns(7);
gridview1.setHorizontalScrollBarEnabled(false);
gridview1.setVerticalScrollBarEnabled(false);
linearlayout1.addView(gridview1);
LinearLayout linearlayout4 = new LinearLayout(context);
LayoutParams layoutparams11 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearlayout4.setLayoutParams(layoutparams11);
linearlayout4.setOrientation(LinearLayout.VERTICAL);
linearlayout4.setVisibility(View.GONE);
framelayout1.addView(linearlayout4);
LinearLayout linearlayout5 = new LinearLayout(context);
LayoutParams layoutparams12 = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 48));
linearlayout5.setLayoutParams(layoutparams12);
linearlayout5.setOrientation(LinearLayout.HORIZONTAL);
linearlayout5.setGravity(Gravity.CENTER_VERTICAL);
linearlayout4.addView(linearlayout5);
TextView textview5 = new TextView(context);
LayoutParams layoutparams13 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview5.setLayoutParams(layoutparams13);
textview5.setText("确定");
textview5.setGravity(Gravity.CENTER_VERTICAL);
textview5.setTextSize(DensityUtil.sp2px(context, 15));
textview5.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
textview5.setTextColor(Color.parseColor("#ffff00"));
linearlayout5.addView(textview5);
LinearLayout linearlayout6 = new LinearLayout(context);
LayoutParams layoutparams14 = new LayoutParams(DensityUtil.dip2px(context, 0),LayoutParams.MATCH_PARENT,1);
linearlayout6.setLayoutParams(layoutparams14);
linearlayout6.setOrientation(LinearLayout.HORIZONTAL);
linearlayout6.setGravity(Gravity.CENTER);
linearlayout5.addView(linearlayout6);
TextView textview6 = new TextView(context);
LayoutParams layoutparams15 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview6.setLayoutParams(layoutparams15);
textview6.setText("1994");
textview6.setGravity(Gravity.CENTER_VERTICAL);
textview6.setPadding(DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0));
linearlayout6.addView(textview6);
TextView textview7 = new TextView(context);
LayoutParams layoutparams16 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
layoutparams16.setMargins(DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
textview7.setLayoutParams(layoutparams16);
textview7.setText("取消");
textview7.setGravity(Gravity.CENTER_VERTICAL);
textview7.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
linearlayout5.addView(textview7);
GridView gridview2 = new GridView(context);
LayoutParams layoutparams17 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutparams17.setMargins(DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
gridview2.setLayoutParams(layoutparams17);
gridview2.setPadding(DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0));
gridview2.setNumColumns(4);
gridview2.setHorizontalScrollBarEnabled(false);
gridview2.setVerticalScrollBarEnabled(false);
linearlayout4.addView(gridview2);
LinearLayout linearlayout7 = new LinearLayout(context);
LayoutParams layoutparams18 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
linearlayout7.setLayoutParams(layoutparams18);
linearlayout7.setOrientation(LinearLayout.VERTICAL);
linearlayout7.setVisibility(View.GONE);
framelayout1.addView(linearlayout7);
LinearLayout linearlayout8 = new LinearLayout(context);
LayoutParams layoutparams19 = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 48));
linearlayout8.setLayoutParams(layoutparams19);
linearlayout8.setOrientation(LinearLayout.HORIZONTAL);
linearlayout8.setGravity(Gravity.CENTER_VERTICAL);
linearlayout7.addView(linearlayout8);
TextView textview8 = new TextView(context);
LayoutParams layoutparams20 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview8.setLayoutParams(layoutparams20);
textview8.setText("←");
textview8.setGravity(Gravity.CENTER_VERTICAL);
textview8.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
linearlayout8.addView(textview8);
LinearLayout linearlayout9 = new LinearLayout(context);
LayoutParams layoutparams21 = new LayoutParams(DensityUtil.dip2px(context, 0),LayoutParams.MATCH_PARENT,1);
linearlayout9.setLayoutParams(layoutparams21);
linearlayout9.setOrientation(LinearLayout.HORIZONTAL);
linearlayout9.setGravity(Gravity.CENTER);
linearlayout8.addView(linearlayout9);
TextView textview9 = new TextView(context);
LayoutParams layoutparams22 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
textview9.setLayoutParams(layoutparams22);
textview9.setText("1994-2015");
textview9.setGravity(Gravity.CENTER_VERTICAL);
textview9.setPadding(DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 5),DensityUtil.dip2px(context, 0));
linearlayout9.addView(textview9);
TextView textview10 = new TextView(context);
LayoutParams layoutparams23 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
layoutparams23.setMargins(DensityUtil.dip2px(context, 5), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
textview10.setLayoutParams(layoutparams23);
textview10.setText("→");
textview10.setGravity(Gravity.CENTER_VERTICAL);
textview10.setPadding(DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 15),DensityUtil.dip2px(context, 0));
linearlayout8.addView(textview10);
GridView gridview3 = new GridView(context);
LayoutParams layoutparams24 = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutparams24.setMargins(DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0), DensityUtil.dip2px(context, 0));
gridview3.setLayoutParams(layoutparams24);
gridview3.setPadding(DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0),DensityUtil.dip2px(context, 0));
gridview3.setNumColumns(4);
gridview3.setHorizontalScrollBarEnabled(false);
gridview3.setVerticalScrollBarEnabled(false);
linearlayout7.addView(gridview3);
TextView textview11 = new TextView(context);
LayoutParams layoutparams25 = new LayoutParams(LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 48));
textview11.setLayoutParams(layoutparams25);
textview11.setText("今日");
textview11.setGravity(Gravity.CENTER);
this.addView(textview11);
}
/**
* dp、sp 转换为 px 的工具类
*
*/
public static class DensityUtil {
/**
* 将px值转换为dip或dp值,保证尺寸大小不变
* @param pxValue
* @param scale (DisplayMetrics类中属性density)
* @return
*/
public static int px2dip(Context context, float pxValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f);
}
/**
* 将dip或dp值转换为px值,保证尺寸大小不变
* @param dipValue
* @param scale(DisplayMetrics类中属性density)
* @return
*/
public static int dip2px(Context context, float dipValue)
{
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
/**
* 将px值转换为sp值,保证文字大小不变
* @param pxValue
* @param fontScale DisplayMetrics类中属性scaledDensity
* @return
*/
public static int px2sp(Context context, float pxValue)
{
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (pxValue / fontScale + 0.5f);
}
/**
* 将sp值转换为px值,保证文字大小不变
*
* @param spValue
* @param fontScale (DisplayMetrics类中属性scaledDensity)
* @return
*/
public static int sp2px(Context context, float spValue)
{
final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
return (int) (spValue * fontScale + 0.5f);
}
}
}
这是被解析的Layout XML文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context="com.xcode.calendarviewdemo.MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/show_layout"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/porvMonth"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="←"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/month"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="九月"/>
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="1994"/>
</LinearLayout>
<TextView
android:id="@+id/nextMonth"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="→"/>
</LinearLayout>
<GridView
android:id="@+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:numColumns="7"
android:padding="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp"
android:paddingTop="0dp"
android:paddingRight="0dp"
android:paddingEnd="0dp"
android:paddingBottom="0dp"
android:scrollbars="none"/>
</LinearLayout>
<LinearLayout
android:id="@+id/month_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/chouse_month"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:textSize="15sp"
android:paddingRight="15dp"
android:textColor="#ffff00"
android:text="确定"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/chouseyear_month"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="1994"/>
</LinearLayout>
<TextView
android:id="@+id/cancle_month"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="取消"/>
</LinearLayout>
<GridView
android:id="@+id/gridview_month"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:numColumns="4"
android:padding="0dp"
android:scrollbars="none"/>
</LinearLayout>
<LinearLayout
android:id="@+id/year_layout"
android:visibility="gone"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="@+id/porv_year"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="←"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="@+id/show_year"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="1994-2015"/>
</LinearLayout>
<TextView
android:id="@+id/next_year"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:gravity="center_vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:text="→"/>
</LinearLayout>
<GridView
android:id="@+id/gridview_year"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="0dp"
android:numColumns="4"
android:padding="0dp"
android:scrollbars="none"/>
</LinearLayout>
</FrameLayout>
<TextView
android:id="@+id/goto_today"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center"
android:text="今日"/>
</LinearLayout>
源文件预览
生成的代码使用方式:
<?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="vertical" >
<com.example.point.testlogin.mView
android:layout_width="match_parent"
android:layout_height="match_parent" >
</com.example.point.testlogin.mView>
</LinearLayout>
自定义控件预览