(转载)Android 用纯代码实现复杂界面
在开发Android应用时有时会遇到纯代码实现复杂界面的需求,本文通过实例来演示,希望能对大家有所帮助
界面截图:

XML布局文件:
1 <?xml version="1.0" encoding="utf-8"?> 2 <ScrollView 3 xmlns:android="http://schemas.android.com/apk/res/android" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:background="@android:color/white"> 7 8 <LinearLayout 9 android:layout_width="fill_parent" 10 android:layout_height="fill_parent" 11 android:orientation="vertical" 12 android:gravity="center"> 13 <ImageView 14 android:layout_width="240dip" 15 android:layout_height="120dip" 16 android:layout_margin="30dip" 17 android:layout_gravity="center_horizontal" 18 android:background="@android:color/black" 19 android:scaleType="fitCenter" 20 android:adjustViewBounds="true" 21 android:src="@android:drawable/ic_dialog_map"/> 22 <TextView 23 android:layout_width="fill_parent" 24 android:layout_height="wrap_content" 25 android:layout_margin="30dip" 26 android:layout_gravity="center_horizontal" 27 android:gravity="center_horizontal" 28 android:textSize="18sp" 29 android:text="测试文本显示"/> 30 <EditText 31 android:layout_width="240dip" 32 android:layout_height="wrap_content" 33 android:layout_margin="30dip" 34 android:layout_gravity="center_horizontal" 35 android:hint="请输入文字内容" 36 android:maxLength="200" 37 android:textSize="18sp"/> 38 <LinearLayout 39 android:id="@+id/button_layout" 40 android:layout_width="240dip" 41 android:layout_height="wrap_content" 42 android:layout_gravity="center_horizontal" 43 android:background="#c6c3c6" 44 android:minHeight="54dip" 45 android:orientation="horizontal" 46 android:paddingTop="4dip" 47 android:paddingBottom="4dip" 48 android:paddingLeft="2dip" 49 android:paddingRight="2dip" > 50 <Button 51 android:text="确定 " 52 android:layout_width="wrap_content" 53 android:layout_height="wrap_content" 54 android:layout_gravity="left" 55 android:layout_marginLeft="10dip" 56 android:layout_marginRight="5dip" 57 android:layout_weight="1" 58 android:maxLines="2" 59 android:textSize="18sp" /> 60 <Button 61 android:text="取消" 62 android:layout_width="wrap_content" 63 android:layout_height="wrap_content" 64 android:layout_gravity="right" 65 android:layout_marginLeft="5dip" 66 android:layout_marginRight="10dip" 67 android:layout_weight="1" 68 android:maxLines="2" 69 android:textSize="18sp"/> 70 </LinearLayout> 71 <RelativeLayout 72 android:layout_width="fill_parent" 73 android:layout_height="wrap_content" 74 > 75 <ImageView 76 android:id="@+id/ImageBottom" 77 android:layout_width="wrap_content" 78 android:layout_height="wrap_content" 79 android:layout_below="@id/button_layout" 80 android:layout_centerHorizontal="true" 81 android:layout_margin="30dip" 82 android:background="#FF777777" 83 android:scaleType="fitCenter" 84 android:adjustViewBounds="true" 85 android:src="@android:drawable/ic_dialog_email"/> 86 </RelativeLayout> 87 88 </LinearLayout> 89 90 </ScrollView>
通过纯代码实现XML同样的效果:
1 import android.app.Activity; 2 import android.content.Context; 3 import android.graphics.Color; 4 import android.os.Bundle; 5 import android.text.InputFilter; 6 import android.text.InputFilter.LengthFilter; 7 import android.view.Gravity; 8 import android.view.ViewGroup; 9 import android.view.ViewGroup.LayoutParams; 10 import android.widget.Button; 11 import android.widget.EditText; 12 import android.widget.ImageView; 13 import android.widget.ImageView.ScaleType; 14 import android.widget.LinearLayout; 15 import android.widget.RelativeLayout; 16 import android.widget.ScrollView; 17 import android.widget.TextView; 18 19 public class ActivityInfo extends Activity { 20 21 @Override 22 protected void onCreate(Bundle savedInstanceState) { 23 // TODO Auto-generated method stub 24 super.onCreate(savedInstanceState); 25 // setContentView(R.layout.info); 26 27 initUI(); 28 } 29 30 public final void initUI(){ 31 ScrollView main = new ScrollView(this); 32 main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 33 main.setBackgroundColor(Color.WHITE); 34 35 //根布局参数 36 LinearLayout.LayoutParams layoutParamsRoot = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT); 37 layoutParamsRoot.gravity = Gravity.CENTER; 38 //根布局 39 LinearLayout layoutRoot = new LinearLayout(this); 40 layoutRoot.setLayoutParams(layoutParamsRoot); 41 layoutRoot.setOrientation(LinearLayout.VERTICAL); 42 43 44 //上边距(dp值) 45 int topMargin = dip2px(this, 30); 46 //imageMain宽度(dp值) 47 int widthMain = dip2px(this, 240); 48 //imageMain高度(dp值) 49 int heightMain = dip2px(this, 120); 50 51 //imageMain布局参数 52 LinearLayout.LayoutParams layoutParamsImageMain = new LinearLayout.LayoutParams(widthMain,heightMain); 53 layoutParamsImageMain.topMargin = topMargin; 54 layoutParamsImageMain.bottomMargin = topMargin; 55 layoutParamsImageMain.leftMargin = topMargin; 56 layoutParamsImageMain.rightMargin = topMargin; 57 layoutParamsImageMain.gravity=Gravity.CENTER_HORIZONTAL; 58 //初始化ImageView 59 ImageView imageMain = new ImageView(this); 60 imageMain.setScaleType(ScaleType.FIT_CENTER); 61 imageMain.setAdjustViewBounds(true); 62 imageMain.setBackgroundColor(Color.BLACK); 63 imageMain.setImageResource(android.R.drawable.ic_dialog_map); 64 layoutRoot.addView(imageMain, layoutParamsImageMain); 65 66 //textInfo布局参数 67 LinearLayout.LayoutParams layoutParamsTextInfo = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 68 layoutParamsTextInfo.topMargin = topMargin; 69 layoutParamsTextInfo.bottomMargin = topMargin; 70 layoutParamsTextInfo.leftMargin = topMargin; 71 layoutParamsTextInfo.rightMargin = topMargin; 72 layoutParamsTextInfo.gravity=Gravity.CENTER_HORIZONTAL; 73 //初始化textInfo 74 TextView textInfo = new TextView(this); 75 textInfo.setGravity(Gravity.CENTER_HORIZONTAL); 76 textInfo.setTextSize(18); 77 layoutRoot.addView(textInfo, layoutParamsTextInfo); 78 79 //editInfo布局参数 80 LinearLayout.LayoutParams layoutParamsEditInfo = new LinearLayout.LayoutParams(widthMain,LayoutParams.WRAP_CONTENT); 81 layoutParamsEditInfo.topMargin = topMargin; 82 layoutParamsEditInfo.gravity=Gravity.CENTER_HORIZONTAL; 83 //初始化editInfo 84 EditText editInfo = new EditText(this); 85 editInfo.setHint("请输入文字内容"); 86 //设置可输入的最大长度 87 InputFilter[] filters = {new LengthFilter(200)}; 88 editInfo.setFilters(filters); 89 editInfo.setTextSize(18); 90 layoutRoot.addView(editInfo, layoutParamsEditInfo); 91 92 //上边距(dp值) 93 int minHeight = dip2px(this, 54); 94 //上padding(dp值) 95 int topPadding = dip2px(this, 4); 96 //左padding(dp值) 97 int leftPadding = dip2px(this, 2); 98 //按钮布局 99 LinearLayout layoutButton = new LinearLayout(this); 100 layoutButton.setLayoutParams(layoutParamsEditInfo); 101 layoutButton.setOrientation(LinearLayout.HORIZONTAL); 102 layoutButton.setBackgroundColor(Color.parseColor("#c6c3c6")); 103 layoutButton.setMinimumHeight(minHeight); 104 layoutButton.setPadding(leftPadding, topPadding, leftPadding, topPadding); 105 layoutButton.setId(100000001); 106 107 //buttonOK布局参数 108 LinearLayout.LayoutParams layoutParamsButtonOK = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 109 layoutParamsButtonOK.gravity = Gravity.LEFT; 110 layoutParamsButtonOK.leftMargin = dip2px(this, 10); 111 layoutParamsButtonOK.rightMargin = dip2px(this, 5); 112 layoutParamsButtonOK.weight = 1; 113 //Button确定 114 Button buttonOK = new Button(this); 115 buttonOK.setLayoutParams(layoutParamsButtonOK); 116 buttonOK.setMaxLines(2); 117 buttonOK.setTextSize(18); 118 buttonOK.setText("确定"); 119 layoutButton.addView(buttonOK); 120 121 //buttonCancel布局参数 122 LinearLayout.LayoutParams layoutParamsButtonCancel = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); 123 layoutParamsButtonCancel.gravity = Gravity.RIGHT; 124 layoutParamsButtonCancel.leftMargin = dip2px(this, 5); 125 layoutParamsButtonCancel.rightMargin = dip2px(this, 10); 126 layoutParamsButtonCancel.weight = 1; 127 //Button取消 128 Button buttonCancel = new Button(this); 129 buttonCancel.setLayoutParams(layoutParamsButtonCancel); 130 buttonCancel.setMaxLines(2); 131 buttonCancel.setTextSize(18); 132 buttonCancel.setText("取消"); 133 134 layoutButton.addView(buttonCancel); 135 136 layoutRoot.addView(layoutButton, layoutParamsEditInfo); 137 138 //RelativeLayout布局参数 139 LinearLayout.LayoutParams layoutParamsBottom = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT); 140 RelativeLayout layoutBottom = new RelativeLayout(this); 141 layoutBottom.setLayoutParams(layoutParamsBottom); 142 143 RelativeLayout.LayoutParams paramsImageBottom = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 144 paramsImageBottom.addRule(RelativeLayout.BELOW, 100000001); 145 paramsImageBottom.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE); 146 paramsImageBottom.setMargins(topMargin, topMargin, topMargin, topMargin); 147 148 //初始化ImageView 149 ImageView imageBottom = new ImageView(this); 150 imageBottom.setScaleType(ScaleType.FIT_CENTER); 151 imageBottom.setAdjustViewBounds(true); 152 imageBottom.setBackgroundColor(0xFF777777); 153 imageBottom.setImageResource(android.R.drawable.ic_dialog_email); 154 layoutBottom.addView(imageBottom, paramsImageBottom); 155 layoutRoot.addView(layoutBottom); 156 157 158 //TODO TEST 159 // imageMain.setBackgroundResource(android.R.drawable.ic_dialog_map); 160 textInfo.setText("测试文本显示"); 161 162 main.addView(layoutRoot); 163 setContentView(main); 164 } 165 166 /** 167 * 根据手机的分辨率从 dp 的单位 转成为 px(像素) 168 */ 169 public static int dip2px(Context context, float dpValue) { 170 final float scale = context.getResources().getDisplayMetrics().density; 171 return (int) (dpValue * scale + 0.5f); 172 } 173 174 /** 175 * 根据手机的分辨率从 px(像素) 的单位 转成为 dp 176 */ 177 public static int px2dip(Context context, float pxValue) { 178 final float scale = context.getResources().getDisplayMetrics().density; 179 return (int) (pxValue / scale + 0.5f); 180 } 181 182 }
浙公网安备 33010602011771号