看到纯代码这个词就说明这篇文章没有任何xml的使用,那要如何去实现这个界面和功能呢?功能的话就先不说了,现在来看看纯代码界面是如何编写的,以及编写的步骤及思想:
1、计算器无非就是多个按钮加上TextView来显示就够了,先看看效果图吧,下面给出多个不同分辨率模拟器上的效果
看到效果的同学要是用水果手机的话肯定似曾相识,没错,就是仿照水果手机上的计算器界面的。下面来看看如何编写界面。
1、创建按钮://创建每一个按钮
private Button myButton(String name)
{
Button button = new Button(this);
button.setHeight(getButtonWidth());
button.setText(name);
button.setTextColor(Color.WHITE);
button.setTextSize(TypedValue.COMPLEX_UNIT_PX,getButtonWidth()/2.5f);
return button;
}
2、把按钮放到LinearLayout容器里面去,首先这里的LinearLayout这要看看你是怎么想的,怎么想就怎么做,我是把每一行的按钮放到一个LinearLayout去的,下图看看我的布局思想:
从图中都看出我的布局思想了,各有各的想法,我这个布局出来 “0“按钮 和 “.”按钮的间隔跟上面的不对齐,这是不标准的,这是我还想还进的地方,其他的还可以,适配不同屏幕也做到按钮是园的而不是只适配一种分辨率,到其他按钮就扁了,所以适配是界面开发的最重要的环节。下面来看看把按钮添加到LinearLayout上去
1、最上层按键://上层LinearLayout
private LinearLayout topLayout()
{
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams layoutp = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT
)
);
layoutp.setMargins(10,10,10,10);
layout.setWeightSum(4);
layoutp.weight=1;
layout.setOrientation(LinearLayout.HORIZONTAL);
String [] name ={"AC","±","%","÷"};
for(int i=0;i<4;i++){
Button button = myButton(name[i]);
buttons.add(button);
button.setLayoutParams(layoutp);
if(i==3) button.setBackground(new MyButtonStyle().symbol_sd());
else {
button.setTextColor(Color.BLACK);
button.setBackground(new MyButtonStyle().gn_sd());
}
layout.addView(button);
}
return layout;
}
2、最底层按键:
//中间三层LinearLayout
private LinearLayout centerLayout(int start,int end,String symbol)
{
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams layoutp = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT
)
);
layoutp.setMargins(10,10,10,10);
layout.setWeightSum(4);
layoutp.weight=1;
// layoutp.width=layoutp.height;
layout.setOrientation(LinearLayout.HORIZONTAL);
for(int i=start;i<
3、最底层按键:
//底层LinearLayout
private LinearLayout bottomLayout()
{
LinearLayout layout = new LinearLayout(this);
LinearLayout.LayoutParams layoutp = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(
0, ViewGroup.LayoutParams.MATCH_PARENT
)
);
layoutp.setMargins(10,10,10,10);
layoutp.weight=1;
LinearLayout.LayoutParams layoutp0 = new LinearLayout.LayoutParams(
new ViewGroup.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT
)
);
layoutp0.setMargins(10,10,10,10);
layoutp0.weight=2;
layout.setWeightSum(4);
layout.setOrientation(LinearLayout.HORIZONTAL);
//0按钮
Button zero_button = new Button(this);
buttons.add(zero_button);
zero_button.setLayoutParams(layoutp0);
zero_button.setText("0");
zero_button.setHeight(getButtonWidth());
zero_button.setGravity(Gravity.CENTER_VERTICAL);
zero_button.setPadding(getButtonWidth()/2-20,0,0,0);
zero_button.setTextColor(Color.WHITE);
zero_button.setTextSize(TypedValue.COMPLEX_UNIT_PX,getButtonWidth()/2.5f);
zero_button.setBackground(new MyButtonStyle().num_sd());
//点按钮
Button dian_button = myButton(".");
buttons.add(dian_button);
dian_button.setLayoutParams(layoutp);
dian_button.setBackground(new MyButtonStyle().num_sd());
//等按钮
Button total_button = myButton("=");
buttons.add(total_button);
total_button.setLayoutParams(layoutp);
total_button.setBackground(new MyButtonStyle().symbol_sd());
//添加到父容器
layout.addView(zero_button);
layout.addView(dian_button);
layout.addView(total_button);
return layout;
}