編輯:關於Android編程
public class WeatherPage extends RelativeLayout{
private Context parentContext;
/**監聽*/
private MyButtonListen mylisten;
/**定義天氣對象*/
private GetWeatherService service;
private RelativeLayout toplayout;
private int toplayoutid=100;
private RelativeLayout centerlayout;
private int centerlayoutid=200;
private RelativeLayout footlayout;
private int footlayoutid=300;
/** topView*/
private ImageView dogpetimg;
private TextView titleview;
private ImageButton switchBtn;
/** showweatherView*/
private RelativeLayout showWeather;
private int showWeatherid=201;
private TextView datetext;
/** functionView*/
private LinearLayout functionLayout;
private TextView selectCitytext;
private Spinner selectProvince;
private ArrayAdapter<CharSequence> adapterProvince;
private Map<String,String> provincemap;
private Spinner selectCity;
private Map<Integer,CityWeather> citymap;
private ArrayAdapter<CharSequence> adapterCity;
private ImageButton okBtn;
private ImageButton selectBtn;
/** 本類對象的樣式 */
private LayoutParams lp;//准備放入ViewFlipper中,所以暫且准備一下,
public WeatherPage(Context context) {
super(context);
this.parentContext=context;
mylisten=new MyButtonListen();
service=new GetWeatherService();
init();
}
private void init() {
toplayout=new RelativeLayout(parentContext);
toplayout.setId(toplayoutid);
LayoutParams lptoplayout=new LayoutParams(-1,-2);
lptoplayout.setMargins(0, 20, 0, 0);
//添加組件
addTopLayout(lptoplayout);
addView(toplayout,lptoplayout);
centerlayout=new RelativeLayout(parentContext);
centerlayout.setId(centerlayoutid);
LayoutParams lpcenterlayout=new LayoutParams(-1,370);
lpcenterlayout.setMargins(0, 30, 0, 0);
lpcenterlayout.addRule(RelativeLayout.BELOW,toplayoutid);
//添加組件
addCenterLayout(lpcenterlayout);
addView(centerlayout,lpcenterlayout);
footlayout=new RelativeLayout(parentContext);
footlayout.setBackgroundColor(Color.RED);
footlayout.setId(footlayoutid);
LayoutParams lpfootlayout=new LayoutParams(-1,-2);
lpfootlayout.setMargins(20, 10, 20, 0);
//添加組件
addFootLayout(lpfootlayout);
lpfootlayout.addRule(RelativeLayout.BELOW,centerlayoutid);
addView(footlayout,lpfootlayout);
}
public LayoutParams getLp() {
this.lp=new LayoutParams(-1,-1);
return lp;
}
public void addTopLayout(LayoutParams lp){
dogpetimg=new ImageView(parentContext);
LayoutParams lpdogpetimg=new LayoutParams(60,60);
lpdogpetimg.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
dogpetimg.setBackgroundResource(R.drawable.dogsmall);
lpdogpetimg.setMargins(10, 0, 0, 0);
titleview=new TextView(parentContext);
titleview.setText("天氣預報");
titleview.setTextColor(Color.BLUE);
LayoutParams lptitleview=new LayoutParams(-2,-2);
lptitleview.addRule(RelativeLayout.CENTER_HORIZONTAL);
switchBtn=new ImageButton(parentContext);
//先進行判斷,判斷原來的開關狀態,然後再添加背景圖片,標記位設置在helper類中。
switchBtn.setBackgroundResource(R.drawable.start);
LayoutParams lpswitchBtn=new LayoutParams(40,40);
lpswitchBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpswitchBtn.setMargins(0, 20, 50, 0);
//添加監聽
switchBtn.setOnClickListener(mylisten);
toplayout.addView(dogpetimg,lpdogpetimg);
toplayout.addView(titleview, lptitleview);
toplayout.addView(switchBtn,lpswitchBtn);
}
public void addCenterLayout(LayoutParams lp) {
showWeather=new RelativeLayout(parentContext);
showWeather.setId(showWeatherid);
LayoutParams lpshowWeather=new LayoutParams(400,300);
lpshowWeather.addRule(RelativeLayout.CENTER_HORIZONTAL);
showWeather.setBackgroundColor(Color.CYAN);
datetext=new TextView(parentContext);
LayoutParams lpdatetext=new LayoutParams(400,-2);
lpdatetext.addRule(RelativeLayout.CENTER_HORIZONTAL);
lpdatetext.addRule(RelativeLayout.BELOW,showWeatherid);
lpdatetext.setMargins(20, 20, 20, 0);
// datetext.setBackgroundColor(Color.LTGRAY);
datetext.setText(TimeHelper.getDateInChina());
datetext.setGravity(Gravity.CENTER_HORIZONTAL);
centerlayout.addView(showWeather, lpshowWeather);
centerlayout.addView(datetext, lpdatetext);
}
public void addFootLayout(LayoutParams lp) {
functionLayout=new LinearLayout(parentContext);
functionLayout.setId(301);
// functionLayout.setBackgroundColor(Color.YELLOW);
LayoutParams lpfunctionLayout=new LayoutParams(-2,-2);
lpfunctionLayout.addRule(RelativeLayout.ALIGN_PARENT_TOP);
lpfunctionLayout.addRule(RelativeLayout.CENTER_HORIZONTAL);
lpfunctionLayout.setMargins(10, 0, 0, 0);
//添加顯示文字
selectCitytext=new TextView(parentContext);
selectCitytext.setText("請設置:");
//添加選擇省
selectProvince=new Spinner(parentContext);
selectProvince.setPrompt("請選擇省份");
//獲取省份Map<序列號,省份對象>
provincemap=service.getProvinceMap();
String[] provinceData=service.getProvinceArray(provincemap);
//定義下拉列表適配器,用於填充內容
adapterProvince=null;
adapterProvince=new ArrayAdapter<CharSequence>(parentContext,android.R.layout.simple_spinner_item,provinceData);
//設置列表顯示風格
adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectProvince.setAdapter(adapterProvince);
selectProvince.setOnItemSelectedListener(new MyOnItemSelectedListen());
//添加選擇市
selectCity=new Spinner(parentContext);
selectCity.setPrompt("請選擇城市");
//定義下拉列表適配器,用於填充內容
adapterCity=null;
//設置列表顯示風格
selectCity.setAdapter(adapterCity);
functionLayout.addView(selectCitytext);
functionLayout.addView(selectProvince);
functionLayout.addView(selectCity);
okBtn=new ImageButton(parentContext);
okBtn.setBackgroundResource(R.drawable.okbtn);//給綁定按鈕添加背景圖片
LayoutParams lpokBtn=new LayoutParams(-2,-2);
lpokBtn.setMargins(20, 20, 0, 0);
lpokBtn.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lpokBtn.addRule(RelativeLayout.BELOW,301);
//添加監聽
okBtn.setOnClickListener(mylisten);
selectBtn=new ImageButton(parentContext);
selectBtn.setBackgroundResource(R.drawable.selectbn);//給查詢按鈕添加背景圖片
LayoutParams lpselectBtn=new LayoutParams(-2,-2);
lpselectBtn.setMargins(0, 20, 20, 0);
lpselectBtn.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
lpselectBtn.addRule(RelativeLayout.BELOW,301);
//添加監聽
selectBtn.setOnClickListener(mylisten);
footlayout.addView(functionLayout,lpfunctionLayout);
footlayout.addView(okBtn, lpokBtn);
footlayout.addView(selectBtn, lpselectBtn);
}
/**
* 監聽各種按鈕的點擊事件
* @author Administrator
*/
class MyButtonListen implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v==switchBtn){
//關閉外顯示功能
System.out.println("點擊外顯示開關");
//換色
switchBtn.setBackgroundResource(R.drawable.end);
}else if(v==okBtn){
//確定,對城市進行保存。寫入小型數據庫
System.out.println("點擊保存開關");
}else if(v==selectBtn){
//確定,對城市進行查詢,不保存
System.out.println("點擊查詢開關");
}else{
Log.e("tag", "問題,輸入的值不對");
}
}
}
/**
* 監聽第一個選項的選擇,當地一個選項框被選擇數據的時候出發該類中的事件
* @author Administrator
*/
class MyOnItemSelectedListen implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> item, View view,
int position, long arg3) {
String provincename = item.getAdapter().getItem(position).toString();
System.out.println(provincename);
String pinyin = provincemap.get(provincename);
Map<String, String> cityIdMap = service.getCityMap(pinyin);
String[] cityArray = service.getCityArray(cityIdMap);
adapterCity=new ArrayAdapter<CharSequence>(parentContext,android.R.layout.simple_spinner_item,cityArray);
//設置列表顯示風格
adapterCity.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectCity.setAdapter(adapterCity);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
開源項目:BottomBar
前言尋尋覓覓終於等到你,Material Design系列BottomBar開源庫你值得擁有。從我接觸android開發遇到tabhost,到radioGroup+Vie
Android拼圖游戲 玩轉從基礎到應用手勢變化
相信大家在小的時候都玩過拼圖游戲,現如今,手機普及,能在手機上玩的游戲越來越多,於是乎,重溫小時候,編寫這個簡易拼圖游戲,而且也能進一步加深Android的一些基礎知識。
Android ImageLoader第三方框架解析
本文實例為大家分享了Android ImageLoader框架的使用方法,供大家參考,具體內容如下1.准備工作1)導入universal-image-loader-1.9
Android 獲取屏幕高度,標題高度,狀態欄高度(實例代碼)
通過View提供的方法獲取高度方式有兩種:1, 當前顯示的view中直接獲取當前view高寬2, 通過Activity的getWindow().findViewById(