---
title: "处女apk纠结过的技术点<4>"
author: "ping8080"
date: "2011-02-28T12:09:53Z"
tags: []
categories: ["移动开发"]
slug: "apk-4"
draft: false
lastmod: "2011-02-28T12:09:53Z"
id: 933310
url: "https://ping8080.iteye.com/blog/933310"
---

listView布局自定义和数据填充，以及结合tabhost使用

1，tabhost定义选项

LayoutInflater.from(this).inflate(R.layout.pressure,  
    host.getTabContentView(), true);  
  // 加载tab选项字体布局  
  LayoutInflater inflater = this.getLayoutInflater();  
  View view1 = inflater.inflate(R.layout.tabview1, null);  
  View view2 = inflater.inflate(R.layout.tabview2, null);  
  View view3 = inflater.inflate(R.layout.tabview3, null);  
  View view4 = inflater.inflate(R.layout.tabview4, null);  
  View view5 = inflater.inflate(R.layout.tabview5, null);  
  View view6 = inflater.inflate(R.layout.tabview6, null);//自定义view填充到选项卡中,改变选项卡风格

  host.addTab(host.newTabSpec("7days").setIndicator(view1)  
    .setContent(R.id.pressure));  
  host.addTab(host.newTabSpec("15days").setIndicator(view2)  
    .setContent(R.id.pressure));  
  host.addTab(host.newTabSpec("30days").setIndicator(view3)  
    .setContent(R.id.pressure));  
  host.addTab(host.newTabSpec("90days").setIndicator(view4)  
    .setContent(R.id.pressure));  
  host.addTab(host.newTabSpec("180days").setIndicator(view5)  
    .setContent(R.id.pressure));  
  host.addTab(host.newTabSpec("360days").setIndicator(view6)  
    .setContent(R.id.pressure));  
  setContentView(host);//设置host为主布局

2，在tabhost选项中设置一直线布局为为显示视图，而布局中填充，列表选项和一个listview

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
 android:layout\_width="fill\_parent"  
 android:layout\_height="fill\_parent"  
 android:orientation="vertical"  
 android:id="@+id/pressure"&gt;  
 &lt;LinearLayout android:layout\_width="fill\_parent"  
  android:layout\_height="wrap\_content" android:orientation="horizontal"&gt;  
  &lt;TextView android:layout\_width="140dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab1" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
  &lt;TextView android:layout\_width="150dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab2" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
  &lt;TextView android:layout\_width="50dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab3" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
  &lt;TextView android:layout\_width="150dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab4" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
  &lt;TextView android:layout\_width="220dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab5" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
  &lt;TextView android:layout\_width="120dip" android:layout\_height="wrap\_content"  
   android:text="@string/tab6" android:textColor="@color/frontcolor"  
   android:textSize="20sp"/&gt;  
 &lt;/LinearLayout&gt;  
 &lt;ListView android:id="@+id/pressureData" android:layout\_width="fill\_parent"  
  android:layout\_height="wrap\_content" android:layout\_weight="1" /&gt;  
&lt;/LinearLayout&gt;

 

tab选项卡中view视图布局

 

 

3，设置listview的布局

 

 

 

&lt;?xml version="1.0" encoding="utf-8"?&gt;  
&lt;LinearLayout xmlns:android="[http://schemas.android.com/apk/res/android](http://schemas.android.com/apk/res/android)"  
 android:layout\_width="fill\_parent" android:layout\_height="fill\_parent"  
 android:orientation="horizontal"&gt;  
 &lt;TextView android:id="@+id/tx1"  android:layout\_width="140dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
 &lt;TextView android:id="@+id/tx2" android:layout\_width="150dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
 &lt;TextView android:id="@+id/tx3" android:layout\_width="50dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
 &lt;TextView android:id="@+id/tx4" android:layout\_width="150dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
 &lt;TextView android:id="@+id/tx5" android:layout\_width="220dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
 &lt;TextView android:id="@+id/tx6" android:layout\_width="120dip"  
  android:layout\_height="wrap\_content" android:text=""  
  android:textColor="@color/white" android:textSize="20sp"/&gt;  
&lt;/LinearLayout&gt;  
主视图下的一个listview中填充的小view

 

4，获取要填充的数据填充到listview中去

 

//自定义适配器

public class PressureAdapter extends BaseAdapter {  
  Context mContext;  
  JSONArray jsonObj;

  public PressureAdapter(Context mContext, JSONArray jsonObj) {  
   super();  
   this.mContext = mContext;  
   this.jsonObj = jsonObj;  
  }

  @Override  
  public int getCount() {  
   return jsonObj.length();  
  }

  @Override  
  public Object getItem(int position) {  
   return position;  
  }

  @Override  
  public long getItemId(int position) {  
   return position;  
  }

  @Override  
  public View getView(int position, View convertView, ViewGroup parent) {  
   LayoutInflater mInflater = getLayoutInflater();  
   View view = mInflater.inflate(R.layout.pressure\_data, null);  
   TextView tx1 = (TextView) view.findViewById(R.id.tx1);//获取listview布局中的view子项ID并且赋值  
   TextView tx2 = (TextView) view.findViewById(R.id.tx2);  
   TextView tx3 = (TextView) view.findViewById(R.id.tx3);  
   TextView tx4 = (TextView) view.findViewById(R.id.tx4);  
   TextView tx5 = (TextView) view.findViewById(R.id.tx5);  
   TextView tx6 = (TextView) view.findViewById(R.id.tx6);  
   try {  
    tx1.setText(jsonObj.getJSONObject(position).getString(  
      "systolic"));  
    tx2.setText(jsonObj.getJSONObject(position).getString(  
      "diastolic"));  
    tx3.setText(jsonObj.getJSONObject(position).getString("pulse"));  
    tx4.setText(jsonObj.getJSONObject(position).getString(  
      "bodyTemperature"));  
    tx5.setText(jsonObj.getJSONObject(position).getString(  
      "dateTime"));  
    tx6.setText(jsonObj.getJSONObject(position).getString("tcText"));  
   } catch (JSONException e) {

    e.printStackTrace();  
   }  
   return view;  
  }  
 }

 

附带：

list = (ListView) findViewById(R.id.pressureData);  
  host.setCurrentTab(0);  //设置当前被选中选项  
  list.setAdapter(createAdapter("7"));  
  host.setOnTabChangedListener(new OnTabChangeListener() {

   @Override  
   public void onTabChanged(String tabId) {//选项切换事件  
    if (host.getCurrentTab() == 0) {           
     list.setAdapter(createAdapter("7"));  
    }  
    if (host.getCurrentTab() == 1) {           
     list.setAdapter(createAdapter("15"));  
    }  
    if (host.getCurrentTab() == 2) {         
     list.setAdapter(createAdapter("30"));  
    }  
    if (host.getCurrentTab() == 3) {       
     list.setAdapter(createAdapter("90"));  
    }  
    if (host.getCurrentTab() == 4) {       
     list.setAdapter(createAdapter("180"));  
    }  
    if (host.getCurrentTab() == 5) {  
     list.setAdapter(createAdapter("360"));  
    }  
   }  
  });