第一次写博客 激动啊 - -!
话不多说了,直接上代码:
HomeActivity代码:
package com.example.lenovo.bottomtab;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Window;
import android.widget.RadioGroup;
public class HomeActivity extends FragmentActivity{
private FragmentManager manager;
private FragmentTransaction transaction;
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
manager = getSupportFragmentManager();
/** * 设置每个RadioButton的监听事件,然后切换对应的Fragment */
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
//开启事务
transaction = manager.beginTransaction();
switch (i) {
case R.id.weixin:
transaction.replace(R.id.content, new WeixinFragment());
break;
case R.id.tongxunlu:
transaction.replace(R.id.content, new TongxunluFragment());
break;
case R.id.friendcircle:
transaction.replace(R.id.content, new FriendFragment());
break;
case R.id.setting:
transaction.replace(R.id.content, new SettingFragment());
break;
}
//事务提交
transaction.commit();
}
});
}
/** * 一进入主页的时候,自动加载第一页tab */
@Override
protected void onStart() {
super.onStart();
radioGroup.check(R.id.weixin);
}
}
接下来是四个Tab选项(在这就写一个,剩下3个大同小异)
public class WeixinFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab_weixin,null);
}
}
首页xml(顶部和底部事先写好 ,然后直接include即可,中部是一个FrameLayout占位,然后transaction.replace()替换即可)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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" android:orientation="vertical" tools:context=".HomeActivity">
<include layout="@layout/top"></include>
<FrameLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1">
</FrameLayout>
<TextView android:layout_width="match_parent" android:layout_height="1px" android:background="#000000" android:layout_marginBottom="5dp"/>
<include layout="@layout/bottom"></include>
</LinearLayout>
这是顶部的ActionBar:
<?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="wrap_content">
<TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="微信" android:textSize="20sp" android:gravity="center_horizontal" android:textStyle="bold" android:paddingTop="10dp" android:paddingBottom="10dp" android:textColor="#ffffff" android:background="#969696"/>
</LinearLayout>
这是底部的RadioGroup
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content">
<RadioGroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal">
<RadioButton android:id="@+id/weixin" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" android:textColor="@color/ziti_color" android:drawableTop="@drawable/weixin_selctor" android:gravity="center_horizontal" android:button="@null" android:text="微信"/>
<RadioButton android:id="@+id/tongxunlu" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" android:textColor="@color/ziti_color" android:drawableTop="@drawable/address_selctor" android:gravity="center_horizontal" android:button="@null" android:text="通讯录"/>
<RadioButton android:id="@+id/friendcircle" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" android:textColor="@color/ziti_color" android:drawableTop="@drawable/find_selctor" android:gravity="center_horizontal" android:button="@null" android:text="朋友圈"/>
<RadioButton android:id="@+id/setting" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:textSize="18sp" android:textColor="@color/ziti_color" android:drawableTop="@drawable/setting_selctor" android:gravity="center_horizontal" android:button="@null" android:text="设置"/>
</RadioGroup>
</LinearLayout>
PS:RadioButton图片跟字体颜色需单独设置selector,代码如下
图片selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/tab_weixin_pressed"></item>
<item android:drawable="@drawable/tab_weixin_normal"></item>
</selector>
字体颜色selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#00ff00" android:state_checked="true"></item>
<item android:color="#969696"></item>
</selector>