一、完成情况
1. 图片
2. 文字介绍
- 点击tab栏底部可以切换不同的页面:
在切换时,被选中的颜色会变化 - 左右滑动屏幕也会切换不同的页面。
3. 代码目录结构
二、代码
1. xml
- tab2.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="@+id/myViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginLeft="0dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/content">
</FrameLayout>
</androidx.viewpager.widget.ViewPager>
<!-- tab栏-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<RelativeLayout
android:id="@+id/rl_tab_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<!-- android:gravity="center"-->
<TextView
android:id="@+id/home_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/home_pur"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/home_text"
android:layout_below="@id/home_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="首页"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_tab_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
>
<TextView
android:id="@+id/display_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/db1_grey"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="@+id/display_text"
android:layout_below="@id/display_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="数据"/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/rl_tab_db"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
>
<TextView
android:id="@+id/db_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/db_grey"
android:layout_centerHorizontal="true"/>
<TextView
android:id="@+id/db_text"
android:layout_below="@id/db_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="折线图"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
- fragment1.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是1"
android:id="@+id/textView3"
android:textSize="18dp" />
</LinearLayout>
- fragment2.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是2"
android:id="@+id/textView3"
android:textSize="18dp" />
</LinearLayout>
- fragment3.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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="我是3"
android:id="@+id/textView3"
android:textSize="18dp" />
</LinearLayout>
2. java
- fragment1.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
View view=inflater .inflate(R.layout.fragment1 ,container,false) ;
return view;
}
}
- fragment2.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
View view=inflater .inflate(R.layout.fragment2 ,container,false) ;
return view;
}
}
- fragment3.java
package com.example.tab;
//import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class fragment1 extends Fragment{
public View onCreateView(LayoutInflater inflater , ViewGroup container, Bundle savedInstanceState){
View view=inflater .inflate(R.layout.fragment3 ,container,false) ;
return view;
}
}
- TabFragmentPagerAdapter.java
package com.example.tab;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import java.util.List;
public class TabFragmentPagerAdapter extends FragmentPagerAdapter {
private FragmentManager mfragmentManager;
private List<Fragment> mlist;
public TabFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.mlist = list;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
}
@Override
public Fragment getItem(int arg0) {
return mlist.get(arg0);//显示第几个页面
}
@Override
public int getCount() {
return mlist.size();//有几个页面
}
}
- main.java
package com.example.tab;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;
import java.util.ArrayList;
import java.util.List;
public class main extends AppCompatActivity implements View.OnClickListener{
private List<Fragment> list;
private ViewPager myViewPager;
private com.example.tab.TabFragmentPagerAdapter adapter;
//定义一些成员变量
TextView mHome;
TextView mDisplay;
TextView mDb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab2);
initView();
}
private void initView()
{
mHome = findViewById(R.id.home_img);
mDisplay = findViewById(R.id.display_img);
mDb = findViewById(R.id.db_img);
RelativeLayout rlHome = findViewById(R.id.rl_tab_home);
RelativeLayout rlDisplay = findViewById(R.id.rl_tab_display);
RelativeLayout rlDb = findViewById(R.id.rl_tab_db);
rlHome.setOnClickListener( this);
rlDisplay.setOnClickListener( this);
rlDb.setOnClickListener( this);
myViewPager = (ViewPager) findViewById(R.id.myViewPager);
//绑定点击事件
myViewPager.setOnPageChangeListener(new MyPagerChangeListener()) ;
//把Fragment添加到List集合里面
list = new ArrayList<>();
list.add(new com.example.tab.fragment1() );
list.add(new com.example.tab.fragment2() );
list.add(new fragment3() );
adapter = new com.example.tab.TabFragmentPagerAdapter(getSupportFragmentManager(), list);
myViewPager.setAdapter(adapter);
myViewPager.setCurrentItem(0); //初始化显示第一个页面
}
@Override
public void onClick(View view) {
int id = view.getId();
switch (id){
//当点击home
case R.id.rl_tab_home:
System.out.println("home被点击了!");
mHome.setBackgroundResource(R.drawable.home_pur);
mDisplay.setBackgroundResource(R.drawable.db1_grey);
mDb.setBackgroundResource(R.drawable.db_grey);
myViewPager.setCurrentItem(0);
break;
case R.id.rl_tab_display:
System.out.println("数据被点击了!");
mHome.setBackgroundResource(R.drawable.home_grey);
mDisplay.setBackgroundResource(R.drawable.db1_pur);
mDb.setBackgroundResource(R.drawable.db_grey);
myViewPager.setCurrentItem(1);
break;
case R.id.rl_tab_db:
System.out.println("折线图被点击了!");
mHome.setBackgroundResource(R.drawable.home_grey);
mDisplay.setBackgroundResource(R.drawable.db1_grey);
mDb.setBackgroundResource(R.drawable.db_pur);
myViewPager.setCurrentItem(2);
break;
}
}
/**
* 设置一个ViewPager的侦听事件,当左右滑动ViewPager时菜单栏被选中状态跟着改变
*
*/
public class MyPagerChangeListener implements ViewPager.OnPageChangeListener {
@Override
public void onPageScrollStateChanged(int arg0) {
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageSelected(int arg0) {
switch (arg0) {//状态改变时底部对应的字体颜色改变
case 0:
mHome.setBackgroundResource(R.drawable.home_pur);
mDisplay.setBackgroundResource(R.drawable.db1_grey);
mDb.setBackgroundResource(R.drawable.db_grey);
break;
case 1:
mHome.setBackgroundResource(R.drawable.home_grey);
mDisplay.setBackgroundResource(R.drawable.db1_pur);
mDb.setBackgroundResource(R.drawable.db_grey);
break;
case 2:
mHome.setBackgroundResource(R.drawable.home_grey);
mDisplay.setBackgroundResource(R.drawable.db1_grey);
mDb.setBackgroundResource(R.drawable.db_pur);
break;
}
}
}
}
3. manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tab">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Tab">
<activity android:name=".MainActivity"
android:exported="true">
</activity>
<activity android:name=".main"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
三、结尾彩蛋~
源码是0积分下载喔~~~
链接点这里