要实现上面的这种底部导航,以前我们都是用tabhost来实现,在android3.0之后android新出了一个fragment,这个比tabhost更简单实现,具体的实现方法,我就直接上代码给大家,很简单的,
首先是xml文件activity_main.xml
<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" >
<LinearLayout
android:id="@+id/main_down_lin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#000000"
>
<TextView
android:id="@+id/main_down_homepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#ffffff"
android:gravity="center"
android:text="首页"
/>
<TextView
android:id="@+id/main_down_mall"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#ffffff"
android:gravity="center"
android:text="商城"
/>
<TextView
android:id="@+id/main_down_circle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#ffffff"
android:gravity="center"
android:text="圈子"
/>
<TextView
android:id="@+id/main_down_admin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:textColor="#ffffff"
android:gravity="center"
android:text="管理"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/main_top_lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/main_down_lin"
>
<fragment
android:id="@+id/main_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.dy.lianxi.fragment.HomePageFragment"
/>
</LinearLayout>
</RelativeLayout>
activity文件MainActivity
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
private HomePageFragment homePageFragment;
private CircleFragment circleFragment;
private AdminFragment adminFragment;
private MallFragment mallFragment;
Fragment curr;//当前的fragment
private TextView main_down_homepage;
private TextView main_down_mall;
private TextView main_down_circle;
private TextView main_down_admin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homePageFragment = new HomePageFragment();
circleFragment = new CircleFragment();
adminFragment = new AdminFragment();
mallFragment = new MallFragment();
initview();
curr = getFragmentManager().findFragmentById(R.id.main_fragment);
setTextColor(main_down_homepage);
main_down_homepage.setOnClickListener(this);
main_down_mall.setOnClickListener(this);
main_down_circle.setOnClickListener(this);
main_down_admin.setOnClickListener(this);
}
private void initview() {
main_down_homepage = (TextView) this.findViewById(R.id.main_down_homepage);
main_down_mall = (TextView) this.findViewById(R.id.main_down_mall);
main_down_circle = (TextView) this.findViewById(R.id.main_down_circle);
main_down_admin = (TextView) this.findViewById(R.id.main_down_admin);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_down_homepage://首页
FragmentSwitch(homePageFragment);
setTextColor(main_down_homepage);
break;
case R.id.main_down_mall://商城
FragmentSwitch(mallFragment);
setTextColor(main_down_mall);
break;
case R.id.main_down_circle://圈子
FragmentSwitch(circleFragment);
setTextColor(main_down_circle);
break;
case R.id.main_down_admin://管理
FragmentSwitch(adminFragment);
setTextColor(main_down_admin);
break;
}
}
//切换fragment
private void FragmentSwitch(Fragment fragment) {
if(curr != fragment){
//开始事物
FragmentTransaction ft = getFragmentManager().beginTransaction();
if(!fragment.isAdded()){
//没找到fragment就添加
ft.add(R.id.main_top_lin, fragment);
}else{
//找到了就显示
ft.show(fragment);
}
//判断当前的fragment是否和传进来的fragment是同一个
if(curr == fragment){
//true 显示fragment
ft.show(fragment);
}else {
//false 隐藏当前的fragment
ft.hide(curr);
}
//关闭事物
ft.commitAllowingStateLoss();
//将fragment的值赋给curr
curr = fragment;
}
}
//变化文字颜色
private void setTextColor(TextView tv) {
main_down_homepage.setTextColor(Color.WHITE);
main_down_mall.setTextColor(Color.WHITE);
main_down_circle.setTextColor(Color.WHITE);
main_down_admin.setTextColor(Color.WHITE);
tv.setTextColor(Color.RED);
}
}
fragment 文件HomePageFragment
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@SuppressLint("NewApi")
public class HomePageFragment extends Fragment {
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_homepage, container, false);
return view;
}
}
资源下载