android tab layout

In this post we need three separate activities for three tab screens. So let’s get started by creating a simple project by opening eclipse IDE.

1. Create a new project File -> New -> Android Project and give activity name asAndroidTabLayoutActivity.
2. Open your AndroidTabLayoutActivity and extend the class from TabActivity.

public class AndroidTabLayoutActivity extends TabActivity {

3. Now open your main.xml under res -> layout folder and type the following code.

<? 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" />
         < FrameLayout
             android:id = "@android:id/tabcontent"
             android:layout_width = "fill_parent"
             android:layout_height = "fill_parent" />
     </ LinearLayout >
</ TabHost >

4. Now we need 3 activities and 3 xml layouts for three tabs. So create three activities by right click on your package folder and create classes and name them asPhotosActivity.javaSongsActivity.java and VideosActivity.java. Type the following code respectively.
Right Click on package folder -> New -> Class

» PhotosActivity.java
package com.example.androidtablayout;
 
import android.app.Activity;
import android.os.Bundle;
 
public class PhotosActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.photos_layout);
     }
}
» SongsActivity.java
package com.example.androidtablayout;
 
import android.app.Activity;
import android.os.Bundle;
 
public class SongsActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.songs_layout);
     }
}
» VideosActivity.java
package com.example.androidtablayout;
 
import android.app.Activity;
import android.os.Bundle;
 
public class VideosActivity extends Activity {
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.videos_layout);
     }
}

5. Now create 3 xml layouts by right clicking on res/layout -> New -> Android XML File and name them as photos_layout.xmlsongs_layout.xml andvideos_layout.xml and type the following code in respective files.

» photos_layout.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent" >
 
   <!-- Screen Design for Photos -->
   < TextView android:text = "PHOTOS HERE"
             android:padding = "15dip"
             android:textSize = "18dip"
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content" />
 
</ LinearLayout >
» songs_layout.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent" >
 
   <!-- Screen Design for the SONGS -->
   < TextView android:text = "SONGS HERE"
             android:padding = "15dip"
             android:textSize = "18dip"
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content" />
</ LinearLayout >
» videos_layout.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< LinearLayout
   android:orientation = "vertical"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent" >
 
   <!--  Screen Design for VIDEOS -->
   < TextView android:text = "VIDEOS HERE"
             android:padding = "15dip"
             android:textSize = "18dip"
             android:layout_width = "fill_parent"
             android:layout_height = "wrap_content" />
</ LinearLayout >

6. Each and every tab needs an icon so design icons for each tab. We need three dimensions of each icon. Design each icon in 48 x 48 px32 x 32 px and 24 x 24 pxand place them in drawable-hdpidrawable-mdpi and drawable-ldpi respectively. See following diagram for your guidance

Android icon sizes

7. Android icon states will be define in xml files with default and hover state configurations. For three icons we need the icon state configuration files. So create three 3 xml files under drawable-hdpi directory. Type the following code for icon states.

» icon_photos_tab.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     <!-- When selected, use grey -->
     < item android:drawable = "@drawable/photos_gray"
           android:state_selected = "true" />
     <!-- When not selected, use white-->
     < item android:drawable = "@drawable/photos_white" />
</ selector >
» icon_songs_tab.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     <!-- When selected, use grey -->
     < item android:drawable = "@drawable/songs_gray"
           android:state_selected = "true" />
     <!-- When not selected, use white-->
     < item android:drawable = "@drawable/songs_white" />
</ selector >
» icon_videos_tab.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< selector xmlns:android = "http://schemas.android.com/apk/res/android" >
     <!-- When selected, use grey -->
     < item android:drawable = "@drawable/videos_gray"
           android:state_selected = "true" />
     <!-- When not selected, use white-->
     < item android:drawable = "@drawable/videos_white" />
</ selector >

8. Now open AndroidTabLayoutActivity.java and type the following code. In the following code we are creating three TabSepcs and adding them to TabHost.

» AndroidTabLayoutActivity.java
package com.example.androidtablayout;
 
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
 
public class AndroidTabLayoutActivity extends TabActivity {
     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super .onCreate(savedInstanceState);
         setContentView(R.layout.main);
 
         TabHost tabHost = getTabHost();
 
         // Tab for Photos
         TabSpec photospec = tabHost.newTabSpec( "Photos" );
         // setting Title and Icon for the Tab
         photospec.setIndicator( "Photos" , getResources().getDrawable(R.drawable.icon_photos_tab));
         Intent photosIntent = new Intent( this , PhotosActivity. class );
         photospec.setContent(photosIntent);
 
         // Tab for Songs
         TabSpec songspec = tabHost.newTabSpec( "Songs" );
         songspec.setIndicator( "Songs" , getResources().getDrawable(R.drawable.icon_songs_tab));
         Intent songsIntent = new Intent( this , SongsActivity. class );
         songspec.setContent(songsIntent);
 
         // Tab for Videos
         TabSpec videospec = tabHost.newTabSpec( "Videos" );
         videospec.setIndicator( "Videos" , getResources().getDrawable(R.drawable.icon_videos_tab));
         Intent videosIntent = new Intent( this , VideosActivity. class );
         videospec.setContent(videosIntent);
 
         // Adding all TabSpec to TabHost
         tabHost.addTab(photospec); // Adding photos tab
         tabHost.addTab(songspec); // Adding songs tab
         tabHost.addTab(videospec); // Adding videos tab
     }
}

9. Now everything is ready and before running your project make sure that you an entry of new activity name in AndroidManifest.xml file. Open you AndroidManifest.xml file and modify the code as below

» AndroidManifest.xml
<? xml version = "1.0" encoding = "utf-8" ?>
< manifest xmlns:android = "http://schemas.android.com/apk/res/android"
       package = "com.example.androidtablayout"
       android:versionCode = "1"
       android:versionName = "1.0" >
     < uses-sdk android:minSdkVersion = "8" />
 
     < application android:icon = "@drawable/icon" android:label = "@string/app_name" >
         < activity android:name = ".AndroidTabLayoutActivity"
                   android:label = "@string/app_name" >
             < intent-filter >
                 < action android:name = "android.intent.action.MAIN" />
                 < category android:name = "android.intent.category.LAUNCHER" />
             </ intent-filter >
         </ activity >
 
         <!--  Songs Activity -->
         < activity android:name = ".SongsActivity" />
 
         <!--  Videos Activity -->
         < activity android:name = ".VideosActivity" />
 
         <!--  Photos Activity -->
         < activity android:name = ".PhotosActivity" />
 
     </ application >
</ manifest >

10. Finally run your project by right clicking on your project folder -> Run As -> 1 Android Application. You will see simple tabs app with awesome navigation.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android Tab 可以使用以下两种样式: 1. 顶部 Tab 样式: 在布局文件中使用 TabLayout 控件,然后在代码中为 TabLayout 添加 Tab,可以设置 Tab 的标题、图标等属性。可以使用 setCustomView() 方法自定义 Tab 的布局。 示例代码: ```xml <com.google.android.material.tabs.TabLayout android:id="@+id/tab_layout" android:layout_width="match_parent" android:layout_height="wrap_content" app:tabGravity="fill" app:tabMode="fixed" /> ``` ```java TabLayout tabLayout = findViewById(R.id.tab_layout); tabLayout.addTab(tabLayout.newTab().setText("Tab1")); tabLayout.addTab(tabLayout.newTab().setText("Tab2")); tabLayout.addTab(tabLayout.newTab().setText("Tab3")); ``` 效果图: ![tab_layout](https://img-blog.csdnimg.cn/20211201160053611.png) 2. 底部 Tab 样式: 可以使用 BottomNavigationView 控件实现底部 Tab 样式,类似于微信、QQ 等应用的底部导航栏。 示例代码: ```xml <com.google.android.material.bottomnavigation.BottomNavigationView android:id="@+id/bottom_navigation_view" android:layout_width="match_parent" android:layout_height="wrap_content" app:menu="@menu/bottom_navigation_menu" /> ``` ```java BottomNavigationView bottomNavigationView = findViewById(R.id.bottom_navigation_view); bottomNavigationView.setOnNavigationItemSelectedListener(item -> { switch (item.getItemId()) { case R.id.menu_home: // 切换到首页 break; case R.id.menu_message: // 切换到消息页面 break; case R.id.menu_mine: // 切换到个人中心页面 break; } return true; }); ``` 效果图: ![bottom_navigation_view](https://img-blog.csdnimg.cn/20211201160230467.png)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值