Android的Tab控件(一)

Tab控件即标签页,可以在一页中切换显示n页内容,要使用此效果,需要用到TabHost和Tabwidget类。

Tab控件具有两种实现过程,一是在同一个Activity中切换显示不同的标签页,二是每个标签页都由独立的Activity实现。我们首先用第二种方法来实现。


新建一个TabHostActivity,它继承自TabActivity,

public class TabHostActivity extends TabActivity{};


他的布局文件tabhostlayout.xml:

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@android:id/tabhost" > <!--注意这里,id的设置方法跟普通控件不同,必须为tabhost -->
    
    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        
        <TabWidget <!--TabWidget控件和FrameLayout控件是必须的,分别用来表示标签和标签下面的内容,同样注意其id选项的设置 -->
        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="wrap_content" />
            
       
    </LinearLayout>
    
</TabHost>


 TabHostActivity类的源码:

public class TabHostActivity extends TabActivity{


Intent intent;
TabHost.TabSpec tabSpec;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tabhostlayout);

Resources res=getResources();

TabHost tabHost=getTabHost();

intent = new Intent(TabHostActivity.this,Tab1Activity.class);
tabSpec = tabHost.newTabSpec("tab1"); //创建一个新的标签页,标记为“tab1”
tabSpec.setIndicator("tab1", res.getDrawable(R.drawable.ic_launcher));//设置tab页的名称和图像表示
tabSpec.setContent(intent);//设置此tab跳转到的Activity
tabHost.addTab(tabSpec);//将此tab加入到tabHost

intent = new Intent(TabHostActivity.this,Tab2Activity.class);
tabSpec=tabHost.newTabSpec("tab2");
tabSpec.setIndicator("tab2", res.getDrawable(R.drawable.ic_launcher));
tabSpec.setContent(intent);
tabHost.addTab(tabSpec);

tabHost.setCurrentTab(1);//设置当期的tab页,从0开始偏移
}


}


然后新建Tab1Activity类,并为其建立布局文件tab1layout.xml,在其中实现tab1页的显示内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <TextView 
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="this is in tab1 !" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />
</LinearLayout>


同样新建Tab2Activity类,并为其建立布局文件tab2layout.xml,在其中实现tab2页的显示内容

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    
    <EditText 
        android:id="@+id/editText"
        android:layout_width="200dp"
        android:layout_height="wrap_content" />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="OK" />
</LinearLayout>

将以上三个Activity添加到manifest文件中即可。


另外一种方法中,各tab都在一个Activity中,在下一篇中进行介绍。


  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值