Android Create tabbed layout settings (notes)

Source: Internet
Author: User

use Tabhost to create tabbed layout settings, switch between different tabs, and display different background images. The effect is as follows:

1, add 6 picture resources (directly dragged into the drawable folder), respectively, Gray.png, White.png, Gray2.png, White2.png, Gray3.png, white3.png represents the default state of the label and the state after the press.

2. Create a new generic XML file in the Drawable folder, Picture.xml, Picture2.xml, Picture3.xml fill in the following code respectively

Picture.xml

<?xml version= "1.0" encoding= "UTF-8"? ><selector  xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item         android:state_selected=" true "        android:drawable=" @drawable/white "/>    <item         android:drawable= "@drawable/gray"/></selector>

Picture2.xml

<?xml version= "1.0" encoding= "UTF-8"? ><selector  xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item         android:state_selected=" true "        android:drawable=" @drawable/white2 "/>    <item         android:drawable= "@drawable/gray2"/></selector>

Picture3.xml

<?xml version= "1.0" encoding= "UTF-8"? ><selector  xmlns:android= "http://schemas.android.com/apk/res/ Android ">     <item         android:state_selected=" true "        android:drawable=" @drawable/white3 "/>    <item         android:drawable= "@drawable/gray3"/></selector>

3. Add the control in Activity_main.xml and call it in Mainactivity.java

Activity_main.xml

<framelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:id=" @+id/container "    android:layout_width=" Match_parent "    Android : layout_height= "match_parent"    tools:context= "com.example.test2.MainActivity"    tools:ignore= " Mergerootframe ">    <textview        android:id=" @+id/view1 "        android:layout_width=" Fill_parent        " android:layout_height= "Wrap_content"         />    <textview        android:id= "@+id/view2"        android: Layout_width= "Fill_parent"        android:layout_height= "wrap_content"         />        <textview        android : id= "@+id/view3"        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        /> </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 (savedinst              Ancestate);        Tabhost tabhost = Gettabhost ();        Layoutinflater.from (This). Inflate (R.layout.activity_main,//Load page Tabhost.gettabcontentview (), true); Tabhost.addtab (Tabhost.newtabspec ("Tab1"). Setindicator ("Tab1", Getresources (). Getdrawable (R.drawable.pi cture)). SetContent (r.id. View1)); Tabhost.addtab (Tabhost.newtabspec ("TaB2"). Setindicator ("TaB2", Getresources (). Getdrawable (R.drawable.pi           Cture2)). SetContent (R.ID.VIEW2)); Tabhost.addtab (Tabhost.newtabspec ("Tab3"). Setindicator ("Tab3", Getresources (). Getdrawable (R.drawable.pi                cture3)). SetContent (R.ID.VIEW3));            Tabhost.setcurrenttab (1); }}
4, the test appears after the situation: only display words do not show the icon, then make the following changes in Androidmanifest.xml, and finally see the icon.


Source code address: http://download.csdn.net/detail/u010499449/8311291

Reprint Please specify Source: http://blog.csdn.net/u010499449/article/details/42213941


Android Create tabbed layout settings (notes)

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.