我们从事Android开发编写布局的时候大多数是使用XML来布局,这给我们带来了方便性,这样操作可以布局界面的代码和逻辑控制的Java代码分离出来,使程序的结构更加清晰、明了。特别的复杂的布局,但是这样操作也同样带来了另一些问题,例如屏幕的适应性,大多数Android开发人员都会遇到这个问题,还有一个就是内容问题,如果使用xml布局,Android的虚拟机首先解析xml布局,然后加载内存,如果布局越复杂,那加载的时间越慢,而用java代码布局,可以解决这些问题,不过比xml布局麻烦一点,而且必须运行才能看见结果。下面讲解一个开发过程中的一个例子:
下面是使用xml布局的代码:
直接在Eclipse里面查看的显示效果如下:
在真机上运行效果如下:
进行对比,真机是我们想要的结果,但是之前的编写的效果不对,而且对于不同的屏幕分辨率xml布局编写了三个xml布局来实现适应性。
下面使用java代码来实现布局,使用两个文件一个布局文件,另一个是布局参数文件
布局文件代码如下:
package com.pcareroute;import com.seuic.pcareroute.AppLog;import com.seuic.pcareroute.surface.CameraSurface;import com.seuic.pcareroute.util.PcareRouteParams;import com.seuic.pcareroute.util.ToolsUnility;import android.app.Activity;import android.os.Bundle;import android.widget.Button;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView;import android.widget.ToggleButton;public class PcareRouteMain extends Activity{ public static final String TAG = "PcareRouteMain"; public PcareRouteMain instance; PcareRouteParams pRouteParams; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ToolsUnility.getToolsUnilityInstance(this).noTitleAndStaBar(this); //去掉标题栏和状态栏 instance = this; pRouteParams = new PcareRouteParams(instance); AppLog.enableLogging(true); initLayout(); } //初始化布局和组件 public void initLayout(){ Parent = new RelativeLayout(getApplicationContext()); Parent.setBackgroundResource(R.drawable.info_back); BottomInParent = new LinearLayout(getApplicationContext()); BottomInParent.setId(_R.id.bottom_in_parent); BottomInParent.setBackgroundResource(R.drawable.button_bar); ButtonInBottom = new ToggleButton[pRouteParams.Bottom_Button_Number]; for (int i = 0; i < pRouteParams.Bottom_Button_Number; i++) { ButtonInBottom[i] = new ToggleButton(getApplicationContext()); ButtonInBottom[i].setBackgroundResource(ButtonInBottomImage[i]); ButtonInBottom[i].setTextOff(" "); ButtonInBottom[i].setTextOn(" "); ButtonInBottom[i].setText(" "); BottomInParent.addView(ButtonInBottom[i], pRouteParams.buttonInBottomParams); } Parent.addView(BottomInParent, pRouteParams.bottomInParentParams); LeftInParent = new RelativeLayout(getApplicationContext()); LeftInParent.setId(_R.id.left_in_parent); BottomInLeft = new LinearLayout(getApplicationContext()); BottomInLeft.setId(_R.id.bottom_in_left); ButtonInLeft = new Button[pRouteParams.Left_Bottom_Button_Number]; for (int i = 0; i < pRouteParams.Left_Bottom_Button_Number; i++) { ButtonInLeft[i] = new Button(getApplicationContext()); ButtonInLeft[i].setText(" "); ButtonInLeft[i].setBackgroundResource(ButtonInLeftImage[i]); BottomInLeft.addView(ButtonInLeft[i], pRouteParams.Left_Bottom_Button_Params); } LeftInParent.addView(BottomInLeft, pRouteParams.bottomInLeftParams); tempView = new TextView(getApplicationContext()); tempView.setTextSize(pRouteParams.Left_Top_Temp_TextSize); tempView.setText("T:"); tempView.setId(_R.id.tempview_in_left); LeftInParent.addView(tempView, pRouteParams.Left_Top_Temp_Params); tempRoll = new ToggleButton(getApplicationContext()); tempRoll.setBackgroundResource(R.drawable.max); tempRoll.setText(" "); tempRoll.setTextOff(" "); tempRoll.setTextOn(" ");// tempRoll.setVisibility(View.INVISIBLE); LeftInParent.addView(tempRoll, pRouteParams.Left_Top_Temp_ToggButton_Params); humidityView = new TextView(getApplicationContext()); humidityView.setText("H:"); humidityView.setId(_R.id.humiview_in_left); humidityView.setTextSize(pRouteParams.Left_Top_Temp_TextSize); LeftInParent.addView(humidityView, pRouteParams.Left_Top_Humi_Params); CenterInLeft = new RelativeLayout(getApplicationContext()); ControlBtn = new Button[CtrolBtnNumber]; for (int i = 0; i < CtrolBtnNumber; i++) { ControlBtn[i] = new Button(getApplicationContext()); ControlBtn[i].setBackgroundResource(Btn_Ctrol_In_Left_Image[i]); CenterInLeft.addView(ControlBtn[i], pRouteParams.Left_Center_CtrolButtons_Params[i]); } LeftInParent.addView(CenterInLeft, pRouteParams.Left_Center_CtrolButton_Params); Parent.addView(LeftInParent, pRouteParams.leftInParentParams); RightInParent = new RelativeLayout(getApplicationContext()); cameraSurface = new CameraSurface(getApplicationContext()); RightInParent.addView(cameraSurface, pRouteParams.Right_Surface_Params); ScaleBtn = new Button[ScaleBtnNumber]; for (int i = 0; i < ScaleBtnNumber; i++) { ScaleBtn[i] = new Button(getApplicationContext()); ScaleBtn[i].setBackgroundResource(Btn_Scale_In_Right_Image[i]); RightInParent.addView(ScaleBtn[i], pRouteParams.Right_Scale_Button_Params[i]); } scaleView = new TextView(getApplicationContext()); scaleView.setText("100%"); scaleView.getPaint().setFakeBoldText(true);//加粗 scaleView.setTextSize(20); RightInParent.addView(scaleView, pRouteParams.Right_Scale_View_Params); Parent.addView(RightInParent, pRouteParams.RightInParentParams); setContentView(Parent, pRouteParams.paramentParams); } //声明组件和布局 public RelativeLayout Parent; public LinearLayout BottomInParent; public RelativeLayout LeftInParent; public LinearLayout BottomInLeft; public RelativeLayout CenterInLeft; public RelativeLayout RightInParent; public CameraSurface cameraSurface; public ToggleButton[] ButtonInBottom; public Button[] ButtonInLeft; public Button[] ControlBtn; public Button[] ScaleBtn; public TextView tempView, humidityView; public TextView scaleView; public ToggleButton tempRoll; //声明变量 public int[] ButtonInBottomImage = {R.drawable.talk_off, R.drawable.music_off, R.drawable.video_off, R.drawable.sound_mute, R.drawable.button_take_photo_icon, R.drawable.ir_off, R.drawable.share_off};//存储底部button的图片的数组 public int[] ButtonInLeftImage = {R.drawable.info_off, R.drawable.net_in_off, R.drawable.wifi_switch_off};//左边布局的下面三个Button按钮图片 public int[] Btn_Ctrol_In_Left_Image = {R.drawable.left_off, R.drawable.up_off, R.drawable.right_off, R.drawable.down_off}; public int CtrolBtnNumber = 4; public int[] Btn_Scale_In_Right_Image = {R.drawable.zoom_out, R.drawable.zoom_in}; public int ScaleBtnNumber = 2;}在上面的代码中如果我们有多个相同的按钮,则可以使用数组来存储,这样做的好处就是代码更加清晰,而且能够减少冗余代码
ControlBtn = new Button[CtrolBtnNumber]; for (int i = 0; i < CtrolBtnNumber; i++) { ControlBtn[i] = new Button(getApplicationContext()); ControlBtn[i].setBackgroundResource(Btn_Ctrol_In_Left_Image[i]); CenterInLeft.addView(ControlBtn[i], pRouteParams.Left_Center_CtrolButtons_Params[i]); }
这样的代码,如果我们用平时的一个组件一个名称,就比现在多余 CtrolBtnNumber倍的代码冗余。
布局参数代码如下:
package com.seuic.pcareroute.util;import com.pcareroute._R;import android.app.Activity;import android.util.DisplayMetrics;import android.view.Gravity;import android.widget.LinearLayout;import android.widget.RelativeLayout;public class PcareRouteParams { public static final String TAG = "PcareRouteParams"; Activity activity; public PcareRouteParams(Activity activity){ this.activity = activity; getDisplayMetrics(); initVar(); initLayoutParams(); } //初始化變量 public void initVar(){ if (screenSize > 5.8) { Bottom_Button_Width = dip2px(60); Left_Bottom_Button_Width = dip2px(35); Bottom_Back_Height = dip2px(70); Left_Top_TopMarge = dip2px(20); Left_Top_Temp_LeftMarge = dip2px(40); Left_Top_Temp_TextSize = 35; Left_Top_Temp_ToggButton_Width = dip2px(28); Left_Top_Temp_ToggButton_Height = dip2px(30); Left_Top_Temp_ToggButton_TopMarge = dip2px(20); Left_Top_Humi_View_TopMarge = dip2px(5); Left_Center_CtrolButton_Width = dip2px(300); Left_Center_CtrolButton_Height = dip2px(250); Left_Center_CtrolButton_LeftMarge = dip2px(20); Left_Center_CtrolButton_BottomMarge = dip2px(20); Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge; Left_Center_CtrolButton_LeftBtn_Width = dip2px(135); Left_Center_CtrolButton_LeftBtn_Height = dip2px(90); Left_Center_CtrolButton_UpBtn_Width = dip2px(100); Left_Center_CtrolButton_UpBtn_Height = dip2px(135); Right_Scale_Button_Width = dip2px(50); Right_Scale_Button_Marge = dip2px(7); Right_Scale_View_BottomMarge = dip2px(40); }else if(screenSize < 3.8){ Bottom_Button_Width = dip2px(35); Left_Bottom_Button_Width = dip2px(18); Bottom_Back_Height = dip2px(35); Left_Top_TopMarge = dip2px(0); Left_Top_Temp_LeftMarge = dip2px(10); Left_Top_Temp_TextSize = 18; Left_Top_Temp_ToggButton_Width = dip2px(15); Left_Top_Temp_ToggButton_Height = dip2px(15); Left_Top_Temp_ToggButton_TopMarge = dip2px(5); Left_Top_Humi_View_TopMarge = dip2px(3); Left_Center_CtrolButton_Width = dip2px(120); Left_Center_CtrolButton_Height = dip2px(120); Left_Center_CtrolButton_LeftMarge = dip2px(20); Left_Center_CtrolButton_BottomMarge = dip2px(5); Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge; Left_Center_CtrolButton_LeftBtn_Width = dip2px(40); Left_Center_CtrolButton_LeftBtn_Height = dip2px(30); Left_Center_CtrolButton_UpBtn_Width = dip2px(30); Left_Center_CtrolButton_UpBtn_Height = dip2px(40); Right_Scale_Button_Width = dip2px(30); Right_Scale_Button_Marge = dip2px(3); Right_Scale_View_BottomMarge = dip2px(15); }else { Bottom_Button_Width = dip2px(40); Left_Bottom_Button_Width = dip2px(27); Bottom_Back_Height = dip2px(45); Left_Top_TopMarge = dip2px(0); Left_Top_Temp_LeftMarge = dip2px(20); Left_Top_Temp_TextSize = 18; Left_Top_Temp_ToggButton_Width = dip2px(15); Left_Top_Temp_ToggButton_Height = dip2px(15); Left_Top_Temp_ToggButton_TopMarge = dip2px(5); Left_Top_Humi_View_TopMarge = dip2px(5); Left_Center_CtrolButton_Width = dip2px(150); Left_Center_CtrolButton_Height = dip2px(150); Left_Center_CtrolButton_LeftMarge = dip2px(20); Left_Center_CtrolButton_BottomMarge = dip2px(10); Right_Surface_LeftMarge = Left_Center_CtrolButton_LeftMarge; Left_Center_CtrolButton_LeftBtn_Width = dip2px(80); Left_Center_CtrolButton_LeftBtn_Height = dip2px(50); Left_Center_CtrolButton_UpBtn_Width = dip2px(50); Left_Center_CtrolButton_UpBtn_Height = dip2px(60); Right_Scale_Button_Width = dip2px(40); Right_Scale_Button_Marge = dip2px(3); Right_Scale_View_BottomMarge = dip2px(18); } Left_Bottom_Button_LeftMarge = dip2px(10); BottomInLeft_BottomMarge = dip2px(5); } public int dip2px(float dpValue) { return (int)(dpValue * scale + 0.5f); } //获取屏幕的宽度,高度和密度以及dp / px public void getDisplayMetrics() { DisplayMetrics dm = new DisplayMetrics(); dm = activity.getApplicationContext().getResources().getDisplayMetrics(); Screen_width = dm.widthPixels; Screen_height = dm.heightPixels; scale = activity.getResources().getDisplayMetrics().density; density = dm.density; double bb = Math.sqrt(Math.pow(Screen_width, 2)+ Math.pow(Screen_height, 2)); screenSize = bb / (160 * dm.density); } //初始化布局参数 public void initLayoutParams(){ paramentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); bottomInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, Bottom_Back_Height); bottomInParentParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); buttonInBottomParams = new LinearLayout.LayoutParams(Bottom_Button_Width, Bottom_Button_Width); buttonInBottomParams.gravity = Gravity.CENTER_VERTICAL; //底部包含组件布局参数 int jiange = (Screen_width - Bottom_Button_Width * Bottom_Button_Number)/(Bottom_Button_Number + 1); buttonInBottomParams.leftMargin = jiange; leftInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT); leftInParentParams.addRule(RelativeLayout.ABOVE, _R.id.bottom_in_parent); bottomInLeftParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT); bottomInLeftParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); bottomInLeftParams.bottomMargin = BottomInLeft_BottomMarge; Left_Bottom_Button_Params = new LinearLayout.LayoutParams(Left_Bottom_Button_Width, Left_Bottom_Button_Width); Left_Bottom_Button_Params.leftMargin = Left_Bottom_Button_LeftMarge; Left_Bottom_Button_Params.gravity = Gravity.CENTER_VERTICAL; Left_Top_Temp_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Left_Top_Temp_Params.topMargin = Left_Top_TopMarge; Left_Top_Temp_Params.leftMargin = Left_Top_Temp_LeftMarge; Left_Top_Temp_ToggButton_Params = new RelativeLayout.LayoutParams(Left_Top_Temp_ToggButton_Width, Left_Top_Temp_ToggButton_Height); Left_Top_Temp_ToggButton_Params.addRule(RelativeLayout.RIGHT_OF, _R.id.tempview_in_left); Left_Top_Temp_ToggButton_Params.topMargin = Left_Top_Temp_ToggButton_TopMarge; Left_Top_Humi_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Left_Top_Humi_Params.addRule(RelativeLayout.BELOW, _R.id.tempview_in_left); Left_Top_Humi_Params.topMargin = Left_Top_Humi_View_TopMarge; Left_Top_Humi_Params.addRule(RelativeLayout.ALIGN_LEFT, _R.id.tempview_in_left); Left_Center_CtrolButton_Params = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_Width, Left_Center_CtrolButton_Height); Left_Center_CtrolButton_Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); Left_Center_CtrolButton_Params.bottomMargin = BottomInLeft_BottomMarge + Left_Center_CtrolButton_BottomMarge + Left_Bottom_Button_Width; Left_Center_CtrolButton_Params.leftMargin = Left_Center_CtrolButton_LeftMarge;// Left_Center_CtrolButton_Params.rightMargin = Left_Center_CtrolButton_LeftMarge; Left_Center_CtrolButtons_Params = new RelativeLayout.LayoutParams[4]; Left_Center_CtrolButtons_Params[0] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Height, Left_Center_CtrolButton_UpBtn_Width); Left_Center_CtrolButtons_Params[0].addRule(RelativeLayout.CENTER_VERTICAL); Left_Center_CtrolButtons_Params[1] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Width, Left_Center_CtrolButton_UpBtn_Height); Left_Center_CtrolButtons_Params[1].addRule(RelativeLayout.CENTER_HORIZONTAL); Left_Center_CtrolButtons_Params[2] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Height, Left_Center_CtrolButton_UpBtn_Width); Left_Center_CtrolButtons_Params[2].addRule(RelativeLayout.ALIGN_PARENT_RIGHT); Left_Center_CtrolButtons_Params[2].addRule(RelativeLayout.CENTER_VERTICAL); Left_Center_CtrolButtons_Params[3] = new RelativeLayout.LayoutParams(Left_Center_CtrolButton_UpBtn_Width, Left_Center_CtrolButton_UpBtn_Height); Left_Center_CtrolButtons_Params[3].addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); Left_Center_CtrolButtons_Params[3].addRule(RelativeLayout.CENTER_HORIZONTAL); RightInParentParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); RightInParentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); RightInParentParams.addRule(RelativeLayout.RIGHT_OF, _R.id.left_in_parent); RightInParentParams.addRule(RelativeLayout.ABOVE, _R.id.bottom_in_parent); RightInParentParams.leftMargin = Right_Surface_LeftMarge; Right_Surface_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT); Right_Scale_Button_Params = new RelativeLayout.LayoutParams[2]; Right_Scale_Button_Params[0] = new RelativeLayout.LayoutParams(Right_Scale_Button_Width, Right_Scale_Button_Width); Right_Scale_Button_Params[0].leftMargin = Right_Scale_Button_Marge; Right_Scale_Button_Params[0].topMargin = Right_Scale_Button_Marge; Right_Scale_Button_Params[1] = new RelativeLayout.LayoutParams(Right_Scale_Button_Width, Right_Scale_Button_Width); Right_Scale_Button_Params[1].addRule(RelativeLayout.ALIGN_PARENT_RIGHT); Right_Scale_Button_Params[1].rightMargin = Right_Scale_Button_Marge; Right_Scale_Button_Params[1].topMargin = Right_Scale_Button_Marge; Right_Scale_View_Params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); Right_Scale_View_Params.addRule(RelativeLayout.CENTER_HORIZONTAL); Right_Scale_View_Params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); Right_Scale_View_Params.bottomMargin = Right_Scale_View_BottomMarge; } //声明布局和组件 public RelativeLayout.LayoutParams paramentParams; public RelativeLayout.LayoutParams bottomInParentParams; public LinearLayout.LayoutParams buttonInBottomParams; public RelativeLayout.LayoutParams leftInParentParams; public RelativeLayout.LayoutParams bottomInLeftParams; public LinearLayout.LayoutParams Left_Bottom_Button_Params; public RelativeLayout.LayoutParams topInLeftParams; public RelativeLayout.LayoutParams Left_Top_Temp_Params; public RelativeLayout.LayoutParams Left_Top_Temp_ToggButton_Params; public RelativeLayout.LayoutParams Left_Top_Humi_Params; public RelativeLayout.LayoutParams Left_Center_CtrolButton_Params; public RelativeLayout.LayoutParams[] Left_Center_CtrolButtons_Params;//左上右下 public RelativeLayout.LayoutParams RightInParentParams; public RelativeLayout.LayoutParams Right_Surface_Params; public RelativeLayout.LayoutParams[] Right_Scale_Button_Params;//大小 public RelativeLayout.LayoutParams Right_Scale_View_Params; //声明变量 public float scale;//dp -- px public double screenSize; public float density; public int Screen_width; public int Screen_height; public int Bottom_Button_Width = 40;//dp public int Bottom_Back_Height = 5;//dp public int Bottom_Button_Number = 7; public int Left_Bottom_Button_Width = 27;//dp public int BottomInLeft_BottomMarge = 5;//5dp public int Left_Bottom_Button_LeftMarge = 10;//dp public int Left_Bottom_Button_Number = 3; public int Left_Top_TopMarge = 5; public int Left_Top_Temp_LeftMarge = 5; public int Left_Top_Temp_TextSize = 18; public int Left_Top_Temp_ToggButton_Width = 15; public int Left_Top_Temp_ToggButton_Height = 15; public int Left_Top_Temp_ToggButton_TopMarge = 5; public int Left_Top_Humi_View_TopMarge = 5; public int Left_Center_CtrolButton_Width = 150; public int Left_Center_CtrolButton_Height = 150; public int Left_Center_CtrolButton_LeftMarge = 20; public int Left_Center_CtrolButton_BottomMarge = 20; public int Right_Surface_LeftMarge = 5; public int Left_Center_CtrolButton_LeftBtn_Width = 80; public int Left_Center_CtrolButton_LeftBtn_Height = 50; public int Left_Center_CtrolButton_UpBtn_Width = 50; public int Left_Center_CtrolButton_UpBtn_Height = 60; public int Right_Scale_Button_Width = 20; public int Right_Scale_Button_Marge = 3; public int Right_Scale_View_BottomMarge = 18;}真机的显示效果如下:
两者虽然在真机上的效果差不多,但是实际是两种不同的实现方式。