我们从事Android开发编写布局的时候大多数是使用XML来布局,这给我们带来了方便性,这样操作可以布局界面的代码和逻辑控制的Java代码分离出来,使程序的结构更加清晰、明了。特别的复杂的布局,但是这样操作也同样带来了另一些问题,例如屏幕的适应性,大多数Android开发人员都会遇到这个问题,还有一个就是内容问题,如果使用xml布局,Android的虚拟机首先解析xml布局,然后加载内存,如果布局越复杂,那加载的时间越慢,而用java代码布局,可以解决这些问题,不过比xml布局麻烦一点,而且必须运行才能看见结果。下面讲解一个开发过程中的一个例子:

下面是使用xml布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:background="@drawable/info_back"><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/linearRecordLayout"><RelativeLayout android:layout_width="wrap_content"android:layout_height="fill_parent"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"><!-- <LinearLayoutandroid:id="@+id/LinearLayout_left" android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical">  --><LinearLayout android:id="@+id/contronLinearLayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_alignParentTop="true"android:layout_marginLeft="20dp"><RelativeLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><TextView android:id="@+id/temp_textview"android:text="T:"android:textSize="18sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFFFFF"            /><ToggleButton android:id="@+id/temp_max"android:layout_width="15dp"android:layout_height="15dp"android:textOn=" " android:textOff=" " android:layout_toRightOf="@+id/temp_textview"android:layout_alignParentTop="true"android:layout_marginTop="5dp"android:background="@drawable/max"android:visibility="invisible"/></RelativeLayout><TextView android:id="@+id/humidity_textiew"android:text="H:"android:textSize="18sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFFFFF"android:layout_marginTop="5dp"/></LinearLayout> <LinearLayout android:id="@+id/BtnControl"android:layout_width="150dp"android:layout_height="150dp"android:orientation="vertical"android:layout_marginTop="20dp"android:layout_alignParentBottom="true"android:layout_marginBottom="35dp"android:gravity="center_vertical|center_horizontal"android:background="@drawable/no_control"><Button android:id="@+id/up"android:layout_width="50dp"android:layout_height="60dp"android:background="@drawable/kong"/><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"><Button android:id="@+id/left"android:layout_width="80dp"android:layout_height="50dp"android:background="@drawable/kong"/><Button android:id="@+id/right"android:layout_width="80dp"android:layout_height="50dp"android:layout_marginLeft="10dp"android:background="@drawable/kong"/></LinearLayout><Button android:id="@+id/down"android:layout_width="50dp"android:layout_height="60dp"android:background="@drawable/kong"/></LinearLayout><!--  </LinearLayout>  --><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_alignParentBottom="true"android:layout_marginBottom="5dp"android:layout_alignParentLeft="true"android:layout_marginLeft="0dp"><Button android:id="@+id/about_button"android:layout_width="27dp"android:layout_height="27dp"android:background="@drawable/info_off"android:layout_marginRight="10dp"/><Button android:id="@+id/webserver"android:layout_width="27dp"android:layout_height="27dp"android:layout_marginRight="10dp"android:background="@drawable/net_in_off"/><Button android:id="@+id/apConvertButton"android:layout_width="27dp"android:layout_height="27dp"android:background="@drawable/wifi_switch_off"/></LinearLayout></RelativeLayout><RelativeLayout android:id="@+id/camerSurfaceRelativelayout"android:layout_width="wrap_content"android:layout_height="wrap_content"><LinearLayout android:layout_width="wrap_content" android:id="@+id/controlAbsoluteLayout" android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"><com.pcareroute.surface.CameraSurfaceViewandroid:id="@+id/car_camera_surfaceview"android:layout_width="wrap_content"android:layout_height="fill_parent" /></LinearLayout><RelativeLayoutandroid:id="@+id/scale_control_linearLayout"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:padding="5dip" ><Buttonandroid:id="@+id/zoom_in_button"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="3dp"android:layout_marginTop="3dp"android:background="@drawable/button_zoom_out_icon" ></Button><LinearLayout android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:layout_marginRight="60dp"android:layout_marginTop="3dp"><ToggleButtonandroid:textOn=" " android:textOff=" " android:id="@+id/record_red"android:layout_width="18dp"android:layout_height="18dp"android:background="@drawable/video_off_led"android:visibility="invisible"/><Chronometerandroid:id="@+id/chronometer1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="7dp"android:text="Chronometer" android:visibility="invisible"/></LinearLayout><Buttonandroid:layout_alignParentRight="true"android:id="@+id/zoom_out_button"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginRight="2dp"android:layout_marginTop="3dp"android:background="@drawable/button_zoom_in_icon"></Button></RelativeLayout><LinearLayoutandroid:id="@+id/RelativeLayoutzoom"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_vertical|center_horizontal"android:layout_centerHorizontal="true"android:layout_above="@+id/bottomEmpty"> <TextViewandroid:id="@+id/scale_textView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center_horizontal"android:shadowColor="#ff000000"android:shadowDx="2"android:shadowDy="0"android:shadowRadius="2"android:text="100%"android:textAppearance="?android:attr/textAppearanceMedium"android:textStyle="bold" /></LinearLayout> <LinearLayout android:id="@+id/bottomEmpty"android:layout_width="fill_parent"android:layout_height="18dp"android:layout_alignParentBottom="true"></LinearLayout></RelativeLayout></LinearLayout> <!-- <RelativeLayoutandroid:id="@+id/scale_control_linearLayout"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentTop="true"android:padding="5dip" ><Buttonandroid:id="@+id/zoom_in_button"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginLeft="25dp"android:background="@drawable/button_zoom_out_icon" ></Button><Buttonandroid:layout_alignParentRight="true"android:id="@+id/zoom_out_button"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginRight="25dp"android:background="@drawable/button_zoom_in_icon" ></Button><LinearLayout android:id="@+id/linearlayout_temp_rh"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_above="@+id/linearRecordLayout"android:layout_centerVertical="true"   ><TextView android:id="@+id/temp_textview"android:text="T"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFFFFF"            android:shadowColor="#ff000000"android:shadowDx="2"android:shadowDy="0"android:shadowRadius="2"/><TextView android:id="@+id/humidity_textiew"android:text="H"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="#FFFFFF"android:layout_marginLeft="100dp"android:shadowColor="#ff000000"android:shadowDx="2"android:shadowDy="0"android:shadowRadius="2"/></LinearLayout><LinearLayoutandroid:id="@+id/RelativeLayoutpower"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@+id/linearbuttonLayout"android:layout_centerHorizontal="true"android:gravity="center_vertical|center_horizontal"android:layout_centerVertical="true"android:layout_toRightOf="@+id/RelativeLayoutzoom"></LinearLayout></RelativeLayout> --> <!-- <LinearLayout  android:orientation="vertical" android:id="@+id/linearControlLayout" android:visibility="visible" android:layout_alignParentRight="true" android:layout_marginRight="10dp"android:layout_width="50dip" android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"><com.wificar.surface.DoubleAxisRightControllerSurfaceView android:layout_gravity="center_vertical"android:clickable="false" android:layout_height="180dp" android:id="@+id/stick_double_axis_right_controller_surfaceview" android:layout_width="wrap_content"></com.wificar.surface.DoubleAxisRightControllerSurfaceView></LinearLayout> <LinearLayout  android:id="@+id/function_linearLayout" android:orientation="vertical" android:layout_width="50dip" android:layout_alignParentLeft="true" android:layout_marginLeft="10dp"android:layout_height="fill_parent" android:gravity="center_vertical|center_horizontal"><com.wificar.surface.DoubleAxisLeftControllerSurfaceView android:layout_gravity="center_vertical"android:clickable="true" android:layout_height="180dp" android:id="@+id/stick_double_axis_left_controller_surfaceview" android:layout_width="wrap_content"></com.wificar.surface.DoubleAxisLeftControllerSurfaceView></LinearLayout>  --><!--  <RelativeLayout android:id="@+id/relativelayout_temp_rh"android:layout_width="fill_parent"android:layout_height="80dp"android:layout_alignParentBottom="true"> --><LinearLayout android:background="@drawable/button_bar" android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_height="45dp" android:id="@+id/linearRecordLayout" android:layout_alignParentBottom="true"><ToggleButton android:id="@+id/send_voice_button" android:layout_width="40dp"android:layout_height="40dp"android:textOn=""android:textOff=""android:background="@drawable/talk_off"android:layout_marginRight="23dip" /><Button android:id="@+id/spk_toggle_button"android:layout_width="40dp"android:layout_height="40dp"android:textOn=" "android:textOff=" "android:background="@drawable/music_off"android:layout_marginRight="23dp"/><ToggleButtonandroid:textOn=" " android:textOff=" " android:layout_marginRight="23dp"  android:layout_width="40dp"android:layout_height="40dp"android:background="@drawable/video_off"android:id="@+id/camera_shoot_button"/><ToggleButton android:background="@drawable/sound_mute" android:id="@+id/listen_toggle_button" android:layout_width="40dp" android:textOn=" " android:textOff=" " android:layout_height="40dp" android:layout_marginRight="23dp" ></ToggleButton><Button android:id="@+id/take_picture_button"android:layout_width="40dp"android:layout_height="40dp"android:layout_marginRight="23dp"  android:background="@drawable/button_take_photo_icon" /><ToggleButton android:background="@drawable/ir_off" android:id="@+id/light_toggle_button" android:layout_width="40dp" android:layout_height="40dp"android:layout_marginRight="23dp" android:textOn=" " android:textOff=" " ></ToggleButton><Button android:background="@drawable/share_off"android:id="@+id/shared"android:layout_width="40dp" android:layout_height="40dp"/></LinearLayout><!-- </RelativeLayout> --></RelativeLayout>

直接在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;@Overrideprotected 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 / pxpublic 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 -- pxpublic double screenSize;public float density;public int Screen_width;public int Screen_height;public int Bottom_Button_Width = 40;//dppublic int Bottom_Back_Height = 5;//dppublic int Bottom_Button_Number = 7;public int Left_Bottom_Button_Width = 27;//dppublic int BottomInLeft_BottomMarge = 5;//5dppublic int Left_Bottom_Button_LeftMarge = 10;//dppublic 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;
}

真机的显示效果如下:

两者虽然在真机上的效果差不多,但是实际是两种不同的实现方式。

转载于:https://www.cnblogs.com/snake-hand/p/3143149.html

Android性能优化---布局优化相关推荐

  1. android布局优化方案,Android启动优化-布局优化

    Android启动优化-布局优化 安卓应用开发发展到今天,已经成为一个非常成熟的技术方向,从目前的情况看,安卓开发还是一个热火朝天的发展,但高级人才却相对较少,如今移动互联网的开发者也逐渐开始注重插入 ...

  2. Android 性能优化——布局优化

    布局优化:就是尽量减少布局文件的层级,致使Android的绘制的工作量减少了,性能就提高啦. 第一种 首先删除布局中无用的控件和层级,其次有选择性的使用性能较低的ViewGroup. 比如Relati ...

  3. 关于android性能,内存优化 http://www.cnblogs.com/zyw-205520/archive/2013/02/17/2914190.html

     随着技术的发展,智能手机硬件配置越来越高,可是它和现在的PC相比,其运算能力,续航能力,存储空间等都还是受到很大的限制,同时用户对手机的体验要  求远远高于PC的桌面应用程序.以上理由,足以需要 ...

  4. Android简单的布局优化

    简单的布局优化 Android系统每隔16ms发出VSYNC信号,触发对UI进行渲染,那么整个过程如果保证在16ms以内就能达到一个流畅的画面. 检测: 1. 设置 -> 开发者选项 -> ...

  5. 性能优化--布局优化技巧

    2019独角兽企业重金招聘Python工程师标准>>> 1.重用布局文件 1.)include标签 首先用得最多的应该是include,按照官方的意思,include就是为了解决重复 ...

  6. Android 布局优化

    在开发过程中我们经常说性能优化,但性能优化是一个比较宽泛的概念.在Android开发中性能优化可能包括:Java代码优化, 算法优化, SQLite优化, 布局优化等.那么这篇博客就来总结并分享下An ...

  7. android 重用布局区分控件,【专题分析】布局优化

    [TOC] # 布局优化 布局优化的思想很简单,就是尽量减少布局文件的层级.主要从以下几个方面入手: * 善于重用布局文件 * 使用ViewStub仅在需要时才加载 * 删除无用的控件和布局 * 使用 ...

  8. 深入探索Android布局优化(上)

    前言 成为一名优秀的Android开发,需要一份完备的知识体系,在这里,让我们一起成长为自己所想的那样~. Android的绘制优化其实可以分为两个部分,即布局(UI)优化和卡顿优化,而布局优化的核心 ...

  9. 史上最全Android性能优化方案解析

    Android中的性能优分为以下几个方面: 布局优化 网络优化 安装包优化 内存优化 卡顿优化 启动优化 -- 一.布局优化 布局优化的本质就是减少View的层级.常见的布局优化方案如下: 在Line ...

  10. Android性能优化系列:启动优化

    文章目录 1 应用启动类型 1.1 冷启动 1.2 温启动 1.3 热启动 2 查看启动耗时 2.1 adb命令查看 2.2 Logcat Displayed查看启动耗时 2.3 手动记录启动耗时 2 ...

最新文章

  1. 检测jQuery.js是否已加载的判断代码
  2. 无法加载JNI共享库(JDK)
  3. 访问外部扩展C语言编程,单片机C语言编程(系统扩展IC)8.ppt
  4. OpenCV 相机校正
  5. C# 关闭正在执行的文件
  6. boost的chrono模块时间间隔的测试程序
  7. 数据库与数据库管理系统的关系
  8. 区县级政府网站群建设要点
  9. Flexible 弹性盒子模型之CSS flex-shrink 属性
  10. 九大神招,让Python里数据分析神器Jupyter,完美升华
  11. php哈希取模,PHP取模hash和一致性hash操作Memcached分布式集群
  12. 关于RNNLM的思考,特别是与HMM,n-gram的区别
  13. 机器学习- 吴恩达Andrew Ng Week8 知识总结 Dimensionality Reduction
  14. 利用简单电阻分压原理自动测量电阻
  15. python中matplotlib的plot函数
  16. 方舟服务器建家位置,《方舟生存进化》五种建家位置!每一种都能暴露建造者的种类!...
  17. SameSite cookie 理解与设置
  18. 新浪微博客户端开发详解-总结(四)
  19. 科学计数法在计算机上怎么表示,科学计数法怎么表示
  20. Mysql主从复制之异步与半同步以及主从切换(实验)

热门文章

  1. 学考计算机删除键是什么字母,考计算机的童鞋注意啦~计算机基础题答案~
  2. 如何用python处理pdf_如何使用Python玩转PDF各种骚操作?
  3. Django初级手册1-项目和应用的创建与简单的数据库操作
  4. bzoj3625:[Codeforces Round #250]小朋友和二叉树
  5. 从零搭建 ES 搜索服务(六)相关性排序优化
  6. 解决ubuntu12.04下安装gitlabError Compiling CSS asset的错误以及401资源错误
  7. 【转】onAttachedToWindow()在整个Activity生命周期的位置及使用
  8. php在类里如何调用call_user_func_array《细说php2》
  9. 关于正则表达式中^和$
  10. 打印图形(内测第1届第1题)