显示效果图:
TabActivity.java:
package com.demo.tab; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager.SimpleOnPageChangeListener; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.TextView; import com.demo.broadcast.R; public class TabActivity extends FragmentActivity implements OnClickListener{ private TextView tab1, tab2, tab3; private ImageView tab1_bottom, tab2_bottom, tab3_bottom; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.tab); initView(); show(1); } private void initView() { tab1 = (TextView) findViewById(R.id.tab1); tab2 = (TextView) findViewById(R.id.tab2); tab3 = (TextView) findViewById(R.id.tab3); tab1.setOnClickListener(this); tab2.setOnClickListener(this); tab3.setOnClickListener(this); tab1_bottom = (ImageView) findViewById(R.id.tab1_bottom); tab2_bottom = (ImageView) findViewById(R.id.tab2_bottom); tab3_bottom = (ImageView) findViewById(R.id.tab3_bottom); viewPager = (ViewPager) findViewById(R.id.viewPager); TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager()); viewPager.setAdapter(adapter); viewPager.setOnPageChangeListener(new SimpleOnPageChangeListener(){ @Override public void onPageSelected(int arg0) { show(arg0); } }); viewPager.setCurrentItem(1); } private void show(int position){ tab1_bottom.setVisibility(position == 0 ? View.VISIBLE : View.INVISIBLE); tab2_bottom.setVisibility(position == 1 ? View.VISIBLE : View.INVISIBLE); tab3_bottom.setVisibility(position == 2 ? View.VISIBLE : View.INVISIBLE); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.tab1: viewPager.setCurrentItem(0); break; case R.id.tab2: viewPager.setCurrentItem(1); break; case R.id.tab3: viewPager.setCurrentItem(2); break; } } }
TabPagerAdapter.java:
package com.demo.tab; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; public class TabPagerAdapter extends FragmentPagerAdapter{ private static int TCOUNT = 3; private TabFragment[] fragments = new TabFragment[TCOUNT]; public TabPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { TabFragment fragment = new TabFragment(); Bundle args = new Bundle(); args.putInt("section_number", position); fragment.setArguments(args); fragments[position] = fragment; return fragment; } @Override public int getCount() { return TCOUNT; } }
TabFragment.java:
package com.demo.tab; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import com.demo.broadcast.R; public class TabFragment extends Fragment{ private int section; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { section = getArguments().getInt("section_number"); if(section == 0){ View view = inflater.inflate(R.layout.tab1, container, false); return view; }else if(section == 1){ View view = inflater.inflate(R.layout.tab2, container, false); return view; }else if(section == 2){ View view = inflater.inflate(R.layout.tab3, container, false); return view; } return null; } }
tab.xml:
<?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:background="#ffffff" android:orientation="vertical" > <android.support.v4.view.ViewPager android:id="@+id/viewPager" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" /> <!-- tab标签栏 --> <LinearLayout android:layout_width="fill_parent" android:layout_height="60dp" android:background="#000000" > <TextView android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="tab1" android:textColor="#ffffff" android:textSize="18sp" /> <View android:layout_width="1dp" android:layout_height="30dp" android:layout_gravity="center_vertical" android:background="#F3F2F6" /> <TextView android:id="@+id/tab2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="tab2" android:textColor="#ffffff" android:textSize="18sp" /> <View android:layout_width="1dp" android:layout_height="30dp" android:layout_gravity="center_vertical" android:background="#F3F2F6" /> <TextView android:id="@+id/tab3" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" android:gravity="center" android:text="tab3" android:textColor="#ffffff" android:textSize="18sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:id="@+id/tab1_bottom" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_red_bottom" /> <ImageView android:id="@+id/tab2_bottom" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_red_bottom" /> <ImageView android:id="@+id/tab3_bottom" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:background="@drawable/tab_red_bottom" /> </LinearLayout> </LinearLayout>
tab1.xml,tab2.xml,tab3.xml布局非常简单,在此就不在展示了。
相关推荐
Fragment的出现,如微信的额主界面包含多个Fragment,使得微信功能更加简洁明了。 Fragment组件 Fragment是Android 3.0的时候被引入的,主要目的是为了给大屏幕(如平板电脑)添加动态和灵活的UI支持。利用Fragment...
什么是Fragment 自从Android 3.0中引入fragments 的概念,根据词海的翻译可以译为:碎片、片段。其目的是为了解决不同屏幕分辩率的动态和灵活UI设计。大屏幕如平板小屏幕如手机,平板电脑的设计使得其有更多的空间...
Android Studio 4.2.1 with Gradle 6.7.1 Fragment界面切换实例。过程说明见:https://blog.csdn.net/lvqing323/article/details/118627931 Fragment是Android3.0后引入的一个新的API,他出现的初衷是为了适应大屏幕...
据说Android最推荐的是在ViewPager中使用FragMent,即ViewPager中的页面不像前面那样用LayoutInflater直接从布局文件加载,而是一个个Fragment。注意这里的Fragment 是android.support.v4.view包里的Fragment,而不是...
流行的应用的导航一般分为两种,一种是底部导航,一种是侧边栏。 说明 IDE:AS,Android studio; 模拟器:genymotion;...中间一个FragmentLayout用来放相应的Fragment; 底部一个大的LinearLayout放着四个样式一样
[四次元]Android学习之数据存储.rar [四次元]android模仿易信UI布局效果源码.zip [四次元]Android轻量级sqlite orm框架.zip [四次元]Fragment实现tab实例 代码.zip [四次元]sqlite的一些基本操作,包括数据库创建、...
一、主界面布局的实现: 先来张效果图: 介绍一下总体界面包括的内容:底部五个导航按钮,主界面包括一个FrameLayout用来放五个Fragment。点击底部按钮会对应跳转到指定的界面。 实现布局:activity_main.xml ...
你可以学习下在Android开发中,tablelayout 与viewpager如何关联,如何创建每个tag标签对应的Fragment,本源码中是创建5个标题并加入布局中,实际应用中,你可以根据需要添加或减少对应的TAb布局数量。 编译时请...
简单的例子,新建一个最基本的Android空白界面,我们得到的是一个可以显示一个空白界面的app。一个activity对应着一个layout。 但是fragment则是基于activity,突破了已经固定好的layout的限制,在原有的layout中...
第9章 使用布局设计用户界面 第10章 使用Fragment 第11章 使用对话框 第四部分 Android应用程序设计精髓 第12章 使用Android首选项 第13章 使用文件和目录 第14章 使用内容提供器 第15章 设计高...
本文实例为大家分享了Android模仿微信主界面展示的具体代码,供大家参考,具体内容如下 先看一下效果图 实现的原理: ViewPager+FragmentPagerAdapter 主界面可分为三部分: top标题栏就是一个TextView 中间的...
HybridApp 一种可以下载的Native App,其用户界面的全部或者部分元素在嵌入式浏览器组件(WebView之类的)里面运行 优雅降级 一开始就构建站点的完整功能,然后针对浏览器测试和修复。认为应该针对那些最高级、最...
本文就给出一个Android开发实例,演示如何搜索SD卡里的文件。 实例界面 首先让那个大家看看程序运行后的界面是什么样子的: 在第一个EditText中设置搜索的目录,默认为根目录”/”。 第二个EditText为所要...
(1)简单写一个主界面的布局activity_main.xml <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:orientation=vertical android...
最近断断续续地把项目的界面部分的代码由JAva改成了Kotlin编写,并且如果应用了kotlin-android-extensions插件,一个显而易见的好处是再也不用写 findViewById()来实例化你的控件对象了,直接操作你在布局文件里的id...