不说废话,直接上代码。activity——main代码:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:id="@+id/tab_title" android:layout_width="match_parent" android:layout_height="48dp" > <TextView android:id="@+id/tv_top" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textSize="18sp" android:textColor="@color/colorBackB" android:autoLink="all" android:text= "Search" /> <!-- 顶部title与子界面的分割线 --> <View android:layout_width="match_parent" android:layout_height="2px" android:background="@color/colorPrimary" android:layout_alignParentBottom="true"/> </RelativeLayout> <LinearLayout android:id="@+id/tab_menu" android:layout_width="match_parent" android:layout_height="56dp" android:orientation="horizontal" android:layout_alignParentBottom="true"> <TextView android:id="@+id/tv_search" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/text_select_bg" android:drawablePadding="3dp" android:gravity="center" android:textColor="@drawable/text_select" android:text="Search"/> <TextView android:id="@+id/tv_review" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/text_select_bg" android:drawablePadding="3dp" android:gravity="center" android:textColor="@drawable/text_select" android:text="Review"/> <TextView android:id="@+id/tv_owner" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/text_select_bg" android:drawablePadding="3dp" android:gravity="center" android:textColor="@drawable/text_select" android:text="Owner"/> <TextView android:id="@+id/tv_setting" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:background="@drawable/text_select_bg" android:drawablePadding="3dp" android:gravity="center" android:textColor="@drawable/text_select" android:text="Setting"/> </LinearLayout> <!-- 导航子界面 --> <FrameLayout android:id="@+id/fl_container" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/tab_title" android:layout_above="@id/tab_menu" > </FrameLayout> </RelativeLayout>
fragment界面,只写出一个fragment_setting.xml,其他类似,不再一一写出:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- 子界面 --> <TextView android:id="@+id/textView3" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/setting" /> </LinearLayout>
MainActivity:
package com.jjw.jjwan.wordmemory; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.TextView; import com.jjw.jjwan.wordmemory.fragment.OwnerFragment; import com.jjw.jjwan.wordmemory.fragment.ReviewFragment; import com.jjw.jjwan.wordmemory.fragment.SearchFragment; import com.jjw.jjwan.wordmemory.fragment.SettingFragment; public class MainActivity extends AppCompatActivity { //顶部title和底部导航 TextView tvTop,tvSearch,tvReview,tvOwner,tvSetting; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvTop = (TextView) findViewById(R.id.tv_top); tvSearch = (TextView) findViewById(R.id.tv_search); tvReview = (TextView) findViewById(R.id.tv_review); tvOwner = (TextView) findViewById(R.id.tv_owner); tvSetting = (TextView) findViewById(R.id.tv_setting); //底部导航注册点击事件 tvSearch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //加载子界面 getSupportFragmentManager().beginTransaction() .replace(R.id.fl_container, new SearchFragment(), "fragment") .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); //改变顶部title tvTop.setText(R.string.search); } }); tvReview.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction() .replace(R.id.fl_container, new ReviewFragment(), "fragment") .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); tvTop.setText(R.string.review); } }); tvOwner.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction() .replace(R.id.fl_container, new OwnerFragment(), "fragment") .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); tvTop.setText(R.string.owner); } }); tvSetting.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction() .replace(R.id.fl_container, new SettingFragment(), "fragment") .setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_FADE) .commit(); tvTop.setText(R.string.setting); } }); } }
SettingFragment.java:
package com.jjw.jjwan.wordmemory.fragment; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; import com.jjw.jjwan.wordmemory.R; public class SettingFragment extends Fragment { private View settingV; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { settingV = inflater.inflate(R.layout.fragment_setting,container,false); return settingV; } @Override public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) { return super.onCreateAnimation(transit, enter, nextAnim); } }其他几个不类似。