TabActivity (1) at the bottom of Android -- FragmentActivity

Source: Internet
Author: User

TabActivity (1) at the bottom of Android -- FragmentActivity

Let's take a look:

The first article in the Tab series first implements the bottom Tab of this style: the background color remains unchanged. We use a dark gray color, and the icon changes accordingly, after a tag is selected, the backplane of the tag changes from normal color to abnormal color. Haha, it turns into a deep gray color, which highlights the effect of the current page, so I will compare this type. I have not processed the text changes here. If the color changes, a selector will be used, so I will not go into details here.


Let's take a look at the structure of the entire Project, as shown below:



Next we will introduce the implementation process one by one. For more information, see annotations. There are not many codes.

Step 1: MainTabActivity. java

Package sun. geoffery. fragmenttabhost; import android. OS. bundle; import android. support. v4.app. fragmentActivity; import android. support. v4.app. fragmentTabHost; import android. view. layoutInflater; import android. view. view; import android. widget. imageView; import android. widget. tabHost. tabSpec; import android. widget. textView;/*** All rights Reserved, Designed By GeofferySun * @ Title: MainTabActivity. java * @ Package sun. geoffery. fragmenttabhost * @ Description: Custom TabHost * @ author: author * @ date: 11:33:15 * @ versionV1.0 */public class MainTabActivity extends FragmentActivity {// define the FragmentTabHost object private FragmentTabHost mTabHost; // define a layout private LayoutInflater mInflater; // define an array to store the Fragment interface private Class mFragmentAry [] = {FragmentPage0.class, FragmentPage1.class, FragmentPage2.class, comment, comment }; // define an array to store the button image private int mImgAry [] = {R. drawable. sl_rbtn_home, R. drawable. sl_rbtn_atme, R. drawable. sl_rbtn_msg, R. drawable. sl_rbtn_square, R. drawable. sl_rbtn_data}; // The text private String mTxtAry [] = {"Homepage", "@ me", "message", "square", "Materials"} on the Tab "}; public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main_tab_layout); initView ();}/*** initialization component */private void initView () {// instantiate the layout object mInflater = LayoutInflater. from (this); // instantiate the TabHost object and get TabHostmTabHost = (FragmentTabHost) findViewById (android. r. id. tabhost); mTabHost. setup (this, getSupportFragmentManager (), R. id. realtabcontent); // get the number of fragment int count = mFragmentAry. length; for (int I = 0; I <count; I ++) {// set the icon, text, and content TabSpec tabSpec = mTabHost for each Tab button. newTabSpec (mTxtAry [I]). setIndicator (getTabItemView (I); // Add the Tab button to the mTabHost Tab. addTab (tabSpec, mFragmentAry [I], null); // set the background of the Tab button mTabHost. getTabWidget (). getChildAt (I ). setBackgroundResource (R. drawable. selector_tab_background) ;}}/*** set the icon and text * @ param index * @ return */private View getTabItemView (int index) {View view = mInflater for the Tab button. inflate (R. layout. tab_item_view, null); ImageView imageView = (ImageView) view. findViewById (R. id. imageview); imageView. setImageResource (mImgAry [index]); TextView textView = (TextView) view. findViewById (R. id. textview); textView. setText (mTxtAry [index]); return view ;}}

Step 2: FragmentPage0.java

package sun.geoffery.fragmenttabhost;import android.os.Bundle;import android.support.v4.app.Fragment;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;public class FragmentPage0 extends Fragment {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {return inflater.inflate(R.layout.fragment_0, null);}}
Step 3: selector_tab_background.xml

 
     
      
  
 
Step 4: Click the label icon sl_rbtn_atme.xml.

 
 
     
      
  
 

Step 5: main_tab_layout.xml

 
 
      <frameLayout        android:id="@+id/realtabcontent"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1" />            <frameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />    
 
Step 6: The layout of each label is tab_item_view.xml, with one icon above and one text below.

 
     
      
  
 
Step 7: fragment_0.xml

   
             
         
 

OK, so far, it will be done. The above steps are not in the development order, but you should be able to understand them. Haha, please leave a message to me.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.