现在的位置: 首页 > 综合 > 正文

Android入门/TabHost(十二)

2018年05月26日 ⁄ 综合 ⁄ 共 7110字 ⁄ 字号 评论关闭
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.widget.FrameLayout
         ↳ android.widget.TabHost

类概述

一个窗口视图的容器。

这个对象包含两个孩子:一个标签页标签,用户点击来选择一个特定的标签,显示该网页的内容,一个FrameLayout对象。
个别元素通常是控制使用这个容器中的对象,而不是设置子元素本身的值。
TabHost控件可以达到分页的效果
UI 布局 activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost	
    android:layout_width="match_parent"
    android:layout_height="match_parent"
   	xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"> 
    
    <TabWidget android:id="@android:id/tabs"  
        android:layout_height="wrap_content"
        android:layout_width="fill_parent">  
	</TabWidget>  
    
    <FrameLayout 
        android:id="@android:id/tabcontent"  
        android:paddingTop="65dp" 
        android:layout_width="match_parent"
        android:layout_height="match_parent"> 
        
        
        <LinearLayout 
            android:layout_height="wrap_content" 
            android:id="@+id/Tab1" 
            android:orientation="horizontal" 
            android:layout_width="fill_parent">  
	              <Button 
		               android:layout_width="fill_parent" 
		               android:layout_height="wrap_content" 
		               android:id="@+id/btnTab1"
		               android:layout_weight="2"
		               android:text="Tab1">
		           </Button>  
		           <EditText 
		                android:layout_height="wrap_content" 
		                android:id="@+id/edtTab1" 
		                android:layout_weight="1"
		                android:textAppearance="@android:attr/autoCompleteTextViewStyle"
		                android:layout_width="fill_parent" 
		                android:inputType="none">               
		           </EditText>  
		         
        </LinearLayout>  
        <LinearLayout 
             android:layout_height="wrap_content" 
             android:id="@+id/Tab2"
             android:layout_width="fill_parent" 
             android:orientation="horizontal">  
		            
			 <GridView
			     xmlns:android="http://schemas.android.com/apk/res/android"
			     android:id="@+id/gridview"
			     android:layout_width="fill_parent"
			     android:layout_height="fill_parent"
			     android:numColumns="auto_fit"
			     android:verticalSpacing="10dp"
			     android:listSelector="@drawable/ic_launcher"
			     android:horizontalSpacing="10dp"
			     android:columnWidth="90dp"
			     android:stretchMode="columnWidth"
			     android:gravity="center" 
			/>
            
           </LinearLayout>  
           
         <RelativeLayout 
             android:layout_height="wrap_content" 
             android:id="@+id/Tab3"
             android:layout_width="fill_parent" 
             android:orientation="horizontal">  
		             <EditText
		                 android:layout_width="fill_parent" 
		                 android:layout_height="wrap_content"
	                     android:textAppearance="@android:attr/autoCompleteTextViewStyle"
		                 android:id="@+id/edtTab3"
		                 android:inputType="none">    
		             </EditText>  
			        <ImageButton 
			            android:id="@+id/imgbtn3"
			            android:layout_width="fill_parent"
			            android:layout_height="wrap_content"
			            android:src="@drawable/ai"
			            android:layout_below="@id/edtTab3"
			            android:contentDescription="@string/app_name"/>
           </RelativeLayout>  
        </FrameLayout>   
</TabHost>

night.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:paddingBottom="4dp"
    android:layout_width="fill_parent" >
    
    <ImageView 
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/ItemImage"
        android:layout_centerHorizontal="true">
    </ImageView>
    <TextView   
               android:layout_width="wrap_content"   
               android:layout_below="@+id/ItemImage"   
               android:layout_height="wrap_content"   
               android:text="TextView"   
               android:layout_centerHorizontal="true"   
               android:id="@+id/ItemText">  
    </TextView>  
    
    
</RelativeLayout>

Java代码

public class MainActivity extends TabActivity {	// TabActivity

	private Button btnTab1;
	private ImageButton imgbtn3;
	private EditText edtTab1,edtTab3;
	public Context context;
	public simple simpleAdapter;
	private GridView gridview;
	private final static String TAB1 = "tab1";
	private final static String TAB2 = "tab2";
	private final static String TAB3 = "tab3";
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        PreUIInit();
        UIInit();
        PostUIInit(); 
        Night();
    }

    private void PreUIInit()
    {
    	setContentView(R.layout.activity_main);
    	// 得到TabHost对象
    	TabHost tabs = getTabHost();
    	//设置Tab1  
    	TabSpec tab1 = tabs.newTabSpec(TAB1);
    	tab1.setIndicator(TAB1);
    	tab1.setContent(R.id.Tab1); // Tab1 --> LinearLayout  @+id/Tab1
    	tabs.addTab(tab1);
    	 //设置Tab2  
        TabSpec tab2 = tabs.newTabSpec(TAB2);  
        tab2.setIndicator(TAB2);        
        tab2.setContent(R.id.Tab2);      
        tabs.addTab(tab2);  
        //设置Tab3 
        TabSpec tab3 = tabs.newTabSpec(TAB3);
        tab3.setIndicator(TAB3);
        tab3.setContent(R.id.Tab3);
        tabs.addTab(tab3);
    }
    
    private void UIInit()
    {
    	// tab1
	    btnTab1=(Button)this.findViewById(R.id.btnTab1);  
        edtTab1=(EditText)this.findViewById(R.id.edtTab1);  
        // tab3
        imgbtn3=(ImageButton)this.findViewById(R.id.imgbtn3);  
        edtTab3=(EditText)this.findViewById(R.id.edtTab3);  
    }
    
    private void PostUIInit()
    {
    	btnTab1.setOnClickListener(listener);
    	imgbtn3.setOnClickListener(listener);
    }
    
    private OnClickListener listener = new OnClickListener() 
    {
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.btnTab1:
				edtTab1.setText(TAB1);
				break;
			case R.id.imgbtn3:
				edtTab3.setText(TAB3);
				break;
			}
			
		}
	};
	
    
    private void Night()
    {
		
				/** 1. 处理好数据 , 生成动态数组	*/		
				gridview = (GridView)findViewById(R.id.gridview);
			
				ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String,Object>>();
				for(int i=0;i<9;i++)
				{
					HashMap<String, Object> map = new HashMap<String, Object>();
					map.put("ItemImage", R.drawable.ic_action_search);
					map.put("ItemText", "No."+i);
					//添加到动态数组
					list.add(map);
				}
				
				/**  2. 生成适配器 	 */			
				simpleAdapter = new simple(this, 
							list,					//数据源
							R.layout.night,			//Items 布局
							new String[]{"ItemImage","ItemText"},// 动态数组list 对应的子项的名称
							new int[]{R.id.ItemImage,R.id.ItemText}// night.xml 内对应组件的id
				);
				
		    	/** 3. gridview添加并且显示
		    	    4.添加消息处理
		    	*/
		    	gridview.setAdapter(simpleAdapter);
				gridview.setOnItemClickListener(Itemlistener);
    		
    }
	// OnItemClickListener 事件处理
	private OnItemClickListener Itemlistener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> parent, 	//AdapterView被点击时发生
								View view, 				//AdapterView内被点击的视图
								int position,			//适配器中的位置的视图
								long id)				//被点击的项行的id
		 {
				@SuppressWarnings("unchecked")
				HashMap<String, Object> item = (HashMap<String, Object>)parent.getItemAtPosition(position);
			    //显示所选Item的ItemText  
				setTitle((String)item.get("ItemText"));
				
				Log.v("ONITEMCLICK","position = "+position);
				simpleAdapter.setSelected(position);
				//simpleAdapter.setContext(context);
		 }
		
	};
	
	/** 内部类  Adapter*/
	public  class simple extends SimpleAdapter{
		
		private List<HashMap<String, Object>> list;
		private int selected = -1;
		//private Context context;
		// 继承父类的构造
		public simple(Context context, List<HashMap<String, Object>> data,
				int resource, String[] from, int[] to) 
		{
			super(context, data, resource, from, to);	
			this.list = data;
		}
		// 获取当前点击的位置
		public void setSelected(int position)
		{
			this.selected = position;
			notifyDataSetChanged();
			Toast.makeText(MainActivity.this, "selected = "+selected, Toast.LENGTH_SHORT).show();
		}
			
		// 获得Items的数量
		public int getCount() 
		{	
			return this.list.size();
		}

		// 根据ListView位置返回View
		public Object getItem(int position)
		{
			return this.list.get(position);
		}

		// 根据ListView位置得到List中的ID
		public long getItemId(int position) 
		{
			return position;
		}
		//	根据位置得到View对象	3个参数[  List中的ID ],[ Items的视图  ],[  父类的视图  ]
		public View getView(int position, View convertView,ViewGroup parent) 
		{
			Log.v("getView", "position = "+position);
			
		
			if(selected == position)
			{
				// 大致作用是 绑定 layout布局文件 ,和findViewById差不多作用,只是一个是找Widget控件 ,一个是找Layout布局文件
				 LayoutInflater inflater =  (LayoutInflater)MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				 convertView = inflater.inflate(R.layout.night, null);
				 
				 ImageView img = (ImageView)convertView.findViewById(R.id.ItemImage);
				 img.setImageResource(R.drawable.ic_launcher);  

				 TextView text = (TextView)convertView.findViewById(R.id.ItemText);
				 text.setText(R.string.app_name);
				 
				 return convertView;
			}
			return super.getView(position, convertView, parent);
		}
		

	} 
    
}

抱歉!评论已关闭.