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

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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns: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"> <!-- <LinearLayout
android: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">
<RelativeLayout
android: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.CameraSurfaceView
android:id="@+id/car_camera_surfaceview"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</LinearLayout>
<RelativeLayout
android:id="@+id/scale_control_linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dip" > <Button
android: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"
>
<ToggleButton
android: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"/>
<Chronometer
android:id="@+id/chronometer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dp"
android:text="Chronometer"
android:visibility="invisible"
/>
</LinearLayout>
<Button
android: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>
<LinearLayout
android: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"
>
<TextView
android: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> <!-- <RelativeLayout
android:id="@+id/scale_control_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dip" > <Button
android: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>
<Button
android: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> <LinearLayout
android: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"
/>
<ToggleButton
android: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;
@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;
}

真机的显示效果如下:

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

Android性能优化---布局优化的更多相关文章

  1. Android 性能优化---布局优化

    Android 性能优化---布局优化 Android 布局绘制原理 布局加载过程 setContentView() --> inflate() -- > getLayout()(I/O操 ...

  2. android view:布局优化

    今天在图书馆看了一个android性能优化. 关于布局优化有几个小技巧: 1.尽量减少布局的嵌套,而使用相对布局,这样的话会减少布局对象的创建,并且可以再事件传递的时候减少传递嵌套. 2.使用incl ...

  3. Android中的布局优化方法

    http://blog.csdn.net/rwecho/article/details/8951009 Android开发中的布局很重要吗?那是当然.一切的显示样式都是由这个布局决定的,你说能不重要吗 ...

  4. Android开发之布局优化

    1.抽象布局标签 (1) <include>标签 include标签经常使用于将布局中的公共部分提取出来供其它layout共用,以实现布局模块化.这在布局编写方便提供了大大的便利. 以下以 ...

  5. [整]Android开发优化-布局优化

    优化布局层次结构 一个普遍的误解就是,使用基本的布局结构会产生高效的布局性能.然而每一个添加到应用的控件和布局,都需要初始化,布局位置和绘制.比如,使用一个嵌套的LinearLayout会导致过深的布 ...

  6. 关于android性能,内存优化

    转:http://www.starming.com/index.php?action=plugin&v=wave&tpl=union&ac=viewgrouppost& ...

  7. android 性能分析、优化

    .主要介绍了一些分析工具,比如GT.ITest等http://www.jianshu.com/p/8b77d394b2a6 .详细介绍啦android平台常见性能优化工具http://blog.csd ...

  8. 我的Android进阶之旅------>Android中的布局优化 include、merge 、ViewStub

    1.如何重用布局文件? 可以使用<include>标签引用其他的布局文件,并用android:id属性覆盖被引用布局文件中顶层节点的android:id属性值.代码如下: <!--引 ...

  9. Android性能优化之中的一个 布局优化

    本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...

随机推荐

  1. 基于visual Studio2013解决算法导论之010快排中应用插入排序

     题目 快排中引用插入排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> ...

  2. bzoj 1030-1039

    1030 JSOI2007 文本生成器 AC自动机加DP即可. 1031 JSOI2007 字符加密Cipher 后缀数组即可. 1032 JSOI2007 祖码Zuma 数据有问题. 设\(f(l, ...

  3. 终于懂了:FWinControls子控件的显示是由Windows来管理,而不是由Delphi来管理(显示透明会导致计算无效区域的方式有所不同——透明的话应减少剪裁区域,所以要进行仔细计算)

    在研究TCustomControl的显示过程中,怎么样都找不到刷新FWinControls并重新显示的代码: procedure TWinControl.PaintHandler(var Messag ...

  4. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  5. iphone开发教程下载

    iphone开发教程下载 3月份花了1个月研究ios 开发,看了几百页的iphone开发教程,累积了不少电子书,盘点一下 Beginning iPhone 4 Development: Explori ...

  6. EDStarRating(IOS星级评定)

    链接地址:https://github.com/erndev/EDStarRating EDStarRating

  7. makefile 必知必会

    Makefile 必知必会 Makefile的根本任务是根据规则生成目标文件. 规则 一条规则包含三个:目标文件,目标文件依赖的文件,更新(或生成)目标文件的命令. 规则: <目标文件>: ...

  8. android设置背景图片透明

    设置Activiyt为透明可以在Activity中引用系统透明主题android:theme="@android:style/Theme.Translucent" 设置背景图片透明 ...

  9. asp.net iis URLRewrite 实现方法详解

    原文 asp.net iis URLRewrite 实现方法详解 实现非常简单首先你要在你的项目里引用两个dll:actionlessform.dll.urlrewriter.dll,真正实现重写的是 ...

  10. 深入理解Arrays.sort() (转)

    Arrays.sort(T[], Comparator < ? super T > c) 方法用于对象数组按用户自定义规则排序.官方Java文档只是简要描述此方法的作用,并未进行详细的介绍 ...