服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - android FragmentTabhost实现导航分页

android FragmentTabhost实现导航分页

2022-07-28 11:30xixi袭阳 Android

这篇文章主要为大家详细介绍了android FragmentTabhost实现导航分页,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android FragmentTabhost导航分页展示的具体代码,供大家参考,具体内容如下

基本模板

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
public class MainActivity extends FragmentActivity {
  
  private FragmentTabHost mTabHost;
  private LayoutInflater mLayoutInflater;
 
  private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
      Fragment3.class, Fragment4.class, Fragment5.class };
 
  private int mImageArray[] = { R.drawable.tab_home_btn,
      R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
      R.drawable.tab_square_btn, R.drawable.tab_more_btn };
 
  private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };
 
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
  }
 
  private void initView() {
 
    mLayoutInflater = LayoutInflater.from(this);
 
    // 找到TabHost
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
    mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
     mTabHost.getTabWidget().setDividerDrawable(null);//去除分割线
    // 得到fragment的个数
    for (int i = 0; i < mFragmentArray.length; i++) {
      // 给每个Tab按钮设置图标、文字和内容
      TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
          .setIndicator(getTabItemView(i));
      // 将Tab按钮添加进Tab选项卡中
      mTabHost.addTab(tabSpec, mFragmentArray[i], null);
      // 设置Tab按钮的背景
      mTabHost.getTabWidget().getChildAt(i)
          .setBackgroundResource(R.drawable.selector_tab_background);
    }
  }
 
  //给每个Tab按钮设置图标和文字
  private View getTabItemView(int index) {
    View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
    ImageView imageView = view.findViewById(R.id.imageview);
    //设置图片选择器,选中的tab改变图标
    switch (index){
      case 0:imageView.setImageResource(R.drawable.main_bottom_image_selector);break;
      case 1:imageView.setImageResource(R.drawable.main_bottom_image_selector2);break;
      case 2:imageView.setImageResource(R.drawable.main_bottom_image_selector3);break;
      case 3:imageView.setImageResource(R.drawable.main_bottom_image_selector4);break;
      case 4:imageView.setImageResource(R.drawable.main_bottom_image_selector5);break;
    }
    TextView textView = view.findViewById(R.id.textview);
    textView.setText(mTextArray[index]);
    //设置文本选择器,选中的tab文字高亮
    textView.setTextColor(R.drawable.main_bottom_text_selector);
    return view;
  }
 
}

acitivity_main.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:orientation="vertical" >
 
 <FrameLayout
  android:id="@+id/realtabcontent"
  android:layout_width="fill_parent"
  android:layout_height="0dip"
  android:layout_weight="1" />
 
 <android.support.v4.app.FragmentTabHost
  android:id="@android:id/tabhost" //必须使用提供的id
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:background="@drawable/bg_tabhost_bg">
 
  <FrameLayout
   android:id="@android:id/tabcontent" //必须使用提供的id
   android:layout_width="0dp"
   android:layout_height="0dp"
   android:layout_weight="0" />     
 </android.support.v4.app.FragmentTabHost>
 
</LinearLayout>

tab_item_view.xml

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:gravity="center"
  android:orientation="vertical">
  <ImageView
    android:id="@+id/imageview"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_margin="3dp"
    />
  <TextView
    android:id="@+id/textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="1dp"
    android:textSize="12sp"
    android:layout_marginBottom="2dp"/>
</LinearLayout>

main_bottom_image_selector图片选择器

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_selected="false" android:drawable="@drawable/ic_launcher" />
  <item android:state_selected="true" android:drawable="@drawable/ic_launcher_round" />
</selector>

 注:模板有5个tab,需要5个图片选择器,还需要5个文本选择器,还需要创建5个fragment。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.cnblogs.com/94xiyang/archive/2018/08/24/9531203.html

延伸 · 阅读

精彩推荐