編輯:關於Android編程
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 MapfieldNameMap = new HashMap (); private static boolean UseDensityUtil = false; public static StringBuilder builder = new StringBuilder(); public static Set importList = new HashSet (); 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 atts = new HashMap (); 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 atts = new HashMap (); 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 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}"); } }
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); } } }
<framelayout android:layout_height="wrap_content" android:layout_width="match_parent"> </framelayout>
源文件預覽
自定義控件預覽
使用PopupWindow來實現彈出框,並且帶有動畫效果首先自定義PopupWindowpublic class LostPopupWindow extends Popu
Android 開源項目第一篇——個性化控件(View)篇包括ListView、ActionBar、Menu、ViewPager、Gallery、
console是構建在stdio之上的,console的初始化是board_r中最後收尾的操作。console的初始化函數console_init_r在common/co
Service概念及用途:Android中的服務,它與Activity不同,它是不能與用戶交互的,不能自己啟動的,運行在後台的程序,如果我們退出應用時,Service進程