来自:http://www.imooc.com/video/5903
可参考另一篇博客http://blog.csdn.net/hnyzwtf/article/details/50278377
1 效果图
这里我们没有实现滑动切换view的功能
2 具体实现:
2.1 布局文件:top.xml, bottom.xml,tab01.xml,tab02.xml,tab03.xml,tab04.xml
具体请参考上述博客
2.2 新建4个Fragment,WeixinFragment,FrdFragment,AddressFragment,SettingFragment,分别对应tab01.xml,tab02.xml,tab03.xml,tab04.xml,其中这个Fragment是android.support.v4.app下面的(为了更好的兼容低版本的安卓设备),此时注意,以下凡是用到的Fragment都要是android.support.v4.app下的,这样不易出问题。
WeixinFragment
package com.example.imooc_weixinfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class WeixinFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab01, container, false);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
FrdFragment
package com.example.imooc_weixinfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FrdFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab02, container, false);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
AddressFragment
package com.example.imooc_weixinfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AddressFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab03, container, false);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
SettingFragment
package com.example.imooc_weixinfragment;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SettingFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//引入我们的布局
return inflater.inflate(R.layout.tab04, container, false);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
activity_main.xml,这里我们使用FrameLayout代替viewPager
<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"
>
<include layout="@layout/top"/>
<FrameLayout
android:id="@+id/id_content"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<include layout="@layout/bottom"/>
</LinearLayout>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
2.3 MainActivity
package com.example.imooc_weixinfragment;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;
//我们使用的是android v4包下的fragment,这里必须要继承自FragmentActivity,而不是Activity
public class MainActivity extends FragmentActivity implements OnClickListener{
//底部的4个导航控件
private LinearLayout mTabWeixin;
private LinearLayout mTabFrd;
private LinearLayout mTabAddress;
private LinearLayout mTabSetting;
//底部4个导航控件中的图片按钮
private ImageButton mImgWeixin;
private ImageButton mImgFrd;
private ImageButton mImgAddress;
private ImageButton mImgSetting;
//初始化4个Fragment
private Fragment tab01;
private Fragment tab02;
private Fragment tab03;
private Fragment tab04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
initView();//初始化所有的view
initEvents();
setSelect(0);//默认显示微信聊天界面
}
private void initEvents() {
mTabWeixin.setOnClickListener(this);
mTabFrd.setOnClickListener(this);
mTabAddress.setOnClickListener(this);
mTabSetting.setOnClickListener(this);
}
private void initView() {
mTabWeixin = (LinearLayout)findViewById(R.id.id_tab_weixin);
mTabFrd = (LinearLayout)findViewById(R.id.id_tab_frd);
mTabAddress = (LinearLayout)findViewById(R.id.id_tab_address);
mTabSetting = (LinearLayout)findViewById(R.id.id_tab_setting);
mImgWeixin = (ImageButton)findViewById(R.id.id_tab_weixin_img);
mImgFrd = (ImageButton)findViewById(R.id.id_tab_frd_img);
mImgAddress = (ImageButton)findViewById(R.id.id_tab_address_img);
mImgSetting = (ImageButton)findViewById(R.id.id_tab_setting_img);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
resetImg();
switch (v.getId()) {
case R.id.id_tab_weixin://当点击微信按钮时,切换图片为亮色,切换fragment为微信聊天界面
setSelect(0);
break;
case R.id.id_tab_frd:
setSelect(1);
break;
case R.id.id_tab_address:
setSelect(2);
break;
case R.id.id_tab_setting:
setSelect(3);
break;
default:
break;
}
}
/*
* 将图片设置为亮色的;切换显示内容的fragment
* */
private void setSelect(int i) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();//创建一个事务
hideFragment(transaction);//我们先把所有的Fragment隐藏了,然后下面再开始处理具体要显示的Fragment
switch (i) {
case 0:
if (tab01 == null) {
tab01 = new WeixinFragment();
/*
* 将Fragment添加到活动中,public abstract FragmentTransaction add (int containerViewId, Fragment fragment)
*containerViewId即为Optional identifier of the container this fragment is to be placed in. If 0, it will not be placed in a container.
* */
transaction.add(R.id.id_content, tab01);//将微信聊天界面的Fragment添加到Activity中
}else {
transaction.show(tab01);
}
mImgWeixin.setImageResource(R.drawable.tab_weixin_pressed);
break;
case 1:
if (tab02 == null) {
tab02 = new FrdFragment();
transaction.add(R.id.id_content, tab02);
}else {
transaction.show(tab02);
}
mImgFrd.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case 2:
if (tab03 == null) {
tab03 = new AddressFragment();
transaction.add(R.id.id_content, tab03);
}else {
transaction.show(tab03);
}
mImgAddress.setImageResource(R.drawable.tab_address_pressed);
break;
case 3:
if (tab04 == null) {
tab04 = new SettingFragment();
transaction.add(R.id.id_content, tab04);
}else {
transaction.show(tab04);
}
mImgSetting.setImageResource(R.drawable.tab_settings_pressed);
break;
default:
break;
}
transaction.commit();//提交事务
}
/*
* 隐藏所有的Fragment
* */
private void hideFragment(FragmentTransaction transaction) {
if (tab01 != null) {
transaction.hide(tab01);
}
if (tab02 != null) {
transaction.hide(tab02);
}
if (tab03 != null) {
transaction.hide(tab03);
}
if (tab04 != null) {
transaction.hide(tab04);
}
}
private void resetImg() {
mImgWeixin.setImageResource(R.drawable.tab_weixin_normal);
mImgFrd.setImageResource(R.drawable.tab_find_frd_normal);
mImgAddress.setImageResource(R.drawable.tab_address_normal);
mImgSetting.setImageResource(R.drawable.tab_settings_normal);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188