Android毕设之Fragment+TabHost(三)

Fragment + TabHost + RadioGroup 结合使用 , 实现底菜单的效果!

1. 使用FragMent是因为 4.0.3之后 ,摒弃了TabActivity这种用法,

Demo 效果图:


先上布局XML R.layout.activity_main

<?xml version="1.0" encoding="utf-8"?>
<TabHost 
    xmlns:android="http://schemas.android.com/apk/res/android"
	android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
    	android:orientation="vertical"
    	android:layout_width="fill_parent"
    	android:layout_height="fill_parent">

    	<TabWidget 
    		android:id="@android:id/tabs"
    		android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:layout_weight="0.0"
    		android:visibility="gone"/>
    	
    	    <include android:id="@+id/header"
			    layout="@layout/my_header"
			    android:layout_height="wrap_content"
			    android:layout_width="fill_parent"/>
			    	
    	<FrameLayout 
    		android:id="@android:id/tabcontent"
    		android:layout_width="fill_parent"
    		android:layout_height="0.0dip"
    		android:layout_weight="1.0">
    
    	    <fragment 
    	        android:name="com.example.coolsmile.fragment.HomeFragment"
    	        android:id="@+id/HomeFragment"
    	        android:layout_width="match_parent"
    	        android:layout_height="match_parent"
    	        />
    	    <fragment 
    	        android:name="com.example.coolsmile.fragment.fragment_tab2"
    	        android:id="@+id/fragment_tab2"
    	        android:layout_width="match_parent"
    	        android:layout_height="match_parent"
    	        />
    	    <fragment 
    	        android:id="@+id/fragment_tab3"
    	        android:name="com.example.coolsmile.fragment.fragment_tab3"
    	        android:layout_width="match_parent"
    	        android:layout_height="match_parent"
    	        />
    	    <fragment 
    	        android:id="@+id/fragment_tab4"
    	        android:name="com.example.coolsmile.fragment.fragment_tab4"
    	        android:layout_width="match_parent"
    	        android:layout_height="match_parent"
    	        />
    	    <fragment 
    	        android:id="@+id/fragment_tab5"
    	        android:name="com.example.coolsmile.fragment.fragment_tab5"
    	        android:layout_width="match_parent"
    	        android:layout_height="match_parent"
    	        />
    	</FrameLayout>
    		 
	    	<RadioGroup
	    		android:id="@+id/main_tab"
	    		android:background="@drawable/bottom1"
	    		android:orientation="horizontal"
	    		android:layout_width="fill_parent"
	    		android:layout_height="wrap_content"
	    		android:gravity="center_vertical"
	    		android:layout_gravity="bottom">
	
	    		<RadioButton
	    		    android:id="@+id/main_tab_home"  
                    style="@style/MMTabButton"  
                    android:layout_weight="1.0"      
                    android:drawableTop="@drawable/menu_icon_0_normal"  
                    android:text="@string/main_home"/>
	    		<RadioButton
	    		    android:id="@+id/main_tab_info"
	    		    style="@style/MMTabButton"
	    		    android:layout_weight="1.0"
	    		    android:drawableTop="@drawable/menu_icon_1_normal"
	    		    android:text="@string/main_my_info" />
	    		<RadioButton
	    		    android:id="@+id/main_tab_news"
	    		    style="@style/MMTabButton"
	    		    android:layout_weight="1.0"
	    		    android:drawableTop="@drawable/menu_icon_2_normal"
	    		    android:text="@string/main_news" />
	    		<RadioButton 
	    			android:id="@+id/main_tab_search"  
                    style="@style/MMTabButton"  
                    android:layout_weight="1.0"  
                    android:drawableTop="@drawable/menu_icon_3_normal"  
                    android:text="@string/main_search"/>
	    		<RadioButton
	    		    android:id="@+id/main_tab_settings"
	    		    style="@style/MMTabButton"
	    		    android:layout_weight="1.0"
	    		    android:drawableTop="@drawable/menu_icon_3_normal"
	    		    android:focusable="false"
	    		    android:text="@string/main_settings" />

	    	</RadioGroup>
	    			

	    	<TextView
	    	    android:id="@+id/main_tab_new_message"
	    	    android:layout_width="wrap_content"
	    	    android:layout_height="wrap_content"
	    	    android:layout_gravity="center_horizontal|top"
	    	    android:layout_marginLeft="15dip"
	    	    android:layout_marginTop="-46dip"
	    	    android:background="@drawable/tips"
	    	    android:gravity="center"
	    	    android:textColor="#ffffff"
	    	    android:textSize="10sp"
	    	    android:visibility="visible" />
  
    </LinearLayout>
</TabHost>

这里有个Fragment标签

android:name="com.example.coolsmile.fragment.HomeFragment"

这里的android:name="具体的Fragment的实现类"


这里我的Acitivty继承了FargmentActivity,onCreate方法里

setContentView(R.layout.activity_main);
InitUI(); 

private void InitUI(){

// get Resource R.string
InitUIString();
// set Message Number
TextView main_tab_new_message=(TextView) findViewById(R.id.main_tab_new_message);
main_tab_new_message.setVisibility(View.VISIBLE);
main_tab_new_message.setText("6");


tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();

tabHost.addTab(
tabHost.newTabSpec(main_home).setIndicator(main_home).setContent(R.id.HomeFragment)
);
tabHost.addTab(
tabHost.newTabSpec(main_my_info).setIndicator(main_my_info).setContent(R.id.fragment_tab2)
);
tabHost.addTab(
tabHost.newTabSpec(main_news).setIndicator(main_news).setContent(R.id.fragment_tab3)
);
tabHost.addTab(
tabHost.newTabSpec(main_search).setIndicator(main_search).setContent(R.id.fragment_tab4)
);
tabHost.addTab(
tabHost.newTabSpec(main_settings).setIndicator(main_settings).setContent(R.id.fragment_tab5)
);

tabHost.setCurrentTab(0);

InitClickListener();
}

private void InitClickListener(){

RadioGroup radioGroup=(RadioGroup) this.findViewById(R.id.main_tab);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
switch (checkedId) {
case R.id.main_tab_home:
tabHost.setCurrentTabByTag(main_home);
break;
case R.id.main_tab_info:
tabHost.setCurrentTabByTag(main_my_info);
break;
case R.id.main_tab_news:
tabHost.setCurrentTabByTag(main_news);
break;
case R.id.main_tab_search:
tabHost.setCurrentTabByTag(main_search);
break;
case R.id.main_tab_settings:
tabHost.setCurrentTabByTag(main_settings);
break;
default:
tabHost.setCurrentTabByTag(main_home);
}
}
});
}
@Override
public void onClick(View v) {


}
public MainHeader getHeader(){
return this.header;
}
private void InitUIString(){

main_home = getResources().getString(R.string.main_home);
main_my_info = getResources().getString(R.string.main_my_info);
main_news = getResources().getString(R.string.main_news);
main_search = getResources().getString(R.string.main_search);
main_settings = getResources().getString(R.string.main_settings);
}

这里实例了一个效果图的HomeFragMent

public class HomeFragment extends Fragment{

	View view;
 
	String[] presidents = { 
		        "Dwight D. Eisenhower", 
		        "John F. Kennedy", 
		        "Lyndon B. Johnson", 
		        "Richard Nixon", 
		        "Gerald Ford", 
		        "Jimmy Carter", 
		        "Ronald Reagan", 
		        "George H. W. Bush", 
		        "Bill Clinton", 
		        "George W. Bush", 
		        "Barack Obama" 
		    }; 
	 
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
				// TODO Auto-generated method stub
				view = inflater.inflate(R.layout.fragment_tab1,container,false);
				Init();
				return view ;
	}
	
	protected void Init(){
				
		ListView list = (ListView)view.findViewById(android.R.id.list);
		
		list.setAdapter(new ArrayAdapter<String>(getActivity(), 
	            android.R.layout.simple_list_item_1, presidents));
		list.setOnItemClickListener(listener);
	}
	
	private OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			Intent intent = new Intent(getActivity(), ListContainActivity.class);
         	
         	intent.putExtra("PRESIDENTS_CONTENT",presidents[position]);

         	getActivity().startActivity(intent);    
		}	
	};
	
}


点击: 下载DEMO







你可能感兴趣的:(Android毕设之Fragment+TabHost(三))