????看到純代碼這個(gè)詞就說明這篇文章沒有任何xml的使用空盼,那要如何去實(shí)現(xiàn)這個(gè)界面和功能呢照雁?功能的話就先不說了,現(xiàn)在來(lái)看看純代碼界面是如何編寫的琐馆,以及編寫的步驟及思想:
? ? 1、計(jì)算器無(wú)非就是多個(gè)按鈕加上TextView來(lái)顯示就夠了恒序,先看看效果圖吧瘦麸,下面給出多個(gè)不同分辨率模擬器上的效果
看到效果的同學(xué)要是用水果手機(jī)的話肯定似曾相識(shí),沒錯(cuò)歧胁,就是仿照水果手機(jī)上的計(jì)算器界面的瞎暑。下面來(lái)看看如何編寫界面。
1与帆、創(chuàng)建按鈕:
//創(chuàng)建每一個(gè)按鈕
? ? 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這要看看你是怎么想的玄糟,怎么想就怎么做,我是把每一行的按鈕放到一個(gè)LinearLayout去的袄秩,下圖看看我的布局思想:
????????從圖中都看出我的布局思想了阵翎,各有各的想法逢并,我這個(gè)布局出來(lái) “0“按鈕 和 “.”按鈕的間隔跟上面的不對(duì)齊,這是不標(biāo)準(zhǔn)的郭卫,這是我還想還進(jìn)的地方砍聊,其他的還可以,適配不同屏幕也做到按鈕是園的而不是只適配一種分辨率贰军,到其他按鈕就扁了玻蝌,所以適配是界面開發(fā)的最重要的環(huán)節(jié)。下面來(lái)看看把按鈕添加到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());
? ? ? ? //點(diǎn)按鈕
? ? ? ? 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;
? ? }