Create a tab layout for Android (note)

Source: Internet
Author: User

Create a tab layout for Android (note)

You can use TabHost to create a label-based layout and switch between different labels to display different background images. The effect is as follows:

11000000006 image resources (gray.png, white.png, gray2.png, white2.png, gray3.png, and white3.png indicate the default label status and the status after the tag is pressed.

2. Create a New XML file in the drawable folder, such as picture. xml, picture2.xml, and picture3.xml. Fill in the following code respectively.

Picture. xml

 

 

Picture2.xml

 

Picture3.xml

 

 
  
   
  
 

3. Add the control in activity_main.xml and call it in MainActivity. java.

Activity_main.xml

 

<framelayout android:id="@+id/container" android:layout_height="match_parent" android:layout_width="match_parent" tools:context="com.example.test2.MainActivity" tools:ignore="MergeRootFrame" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
 
          
   
  
 </framelayout>
MainActivity. xml

 

 

Package com. example. test3; import android. app. activity; import android. app. actionBar; import android. app. fragment; import android. app. tabActivity; import android. content. intent; import android. content. res. resources; import android. graphics. drawable. drawable; import android. OS. bundle; import android. view. layoutInflater; import android. view. menu; import android. view. menuItem; import android. view. view; import android. view. viewGroup; import android. widget. tabHost; import android. widget. tabHost. tabSpec; import android. OS. build; public class MainActivity extends TabActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); TabHost tabHost = getTabHost (); LayoutInflater. from (this ). inflate (R. layout. activity_main, // load the page tabHost. getTabContentView (), true); tabHost. addTab (tabHost. newTabSpec (tab1 ). setIndicator (tab1, getResources (). getDrawable (R. drawable. picture )). setContent (R. id. view1); tabHost. addTab (tabHost. newTabSpec (tab2 ). setIndicator (tab2, getResources (). getDrawable (R. drawable. picture2 )). setContent (R. id. view2); tabHost. addTab (tabHost. newTabSpec (tab3 ). setIndicator (tab3, getResources (). getDrawable (R. drawable. picture3 )). setContent (R. id. view3); tabHost. setCurrentTab (1 );}}
4. After the test, if the icon is not displayed, the following changes are made in AndroidManifest. xml to see the icon.

 

 

 

 

Related Article

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.