新建页面并编写
#第一个框架处理,分为top,content,bottom
事件处理函数
MainActivity.java
package com.example.wexin;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Fragment wexin_bar = new WexinFragment();
private Fragment friend_bar = new FriendFragment();
private Fragment contact_bar = new ContactFragment();
private Fragment setting_bar = new SettingFragment();
private FragmentManager fm;
LinearLayout mTabWexin,mTabFriend,mTabContact,mTabSetting;
ImageButton mBtn_01,mBtn_02,mBtn_03,mBtn_04;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置没有
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
intView();
initEvent();
initFragment();
setSelectFragment(0);
}
// 初始化我的fragment
private void initFragment(){
fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fl_01,wexin_bar);
transaction.add(R.id.fl_01,friend_bar);
transaction.add(R.id.fl_01,contact_bar);
transaction.add(R.id.fl_01,setting_bar);
transaction.commit();
}
// 找到相应区
private void intView(){
mTabWexin = findViewById(R.id.lay_01);
mTabFriend = findViewById(R.id.lay_02);
mTabContact = findViewById(R.id.lay_03);
mTabSetting = findViewById(R.id.lay_04);
mBtn_01 = findViewById(R.id.btn_01);
mBtn_02 = findViewById(R.id.btn_02);
mBtn_03 = findViewById(R.id.btn_03);
mBtn_04 = findViewById(R.id.btn_04);
}
//根据i值选择
private void setSelectFragment(int i){
FragmentTransaction transaction = fm.beginTransaction();
//隐藏四个主体内容
hideFragment(transaction);
//设置内容为亮的部分
switch (i){
case 0:
transaction.show(wexin_bar);
mBtn_01.setImageResource(R.drawable.tab_weixin_pressed);
break;
case 1:
transaction.show(friend_bar);
mBtn_02.setImageResource(R.drawable.tab_find_frd_pressed);
break;
case 2:
transaction.show(contact_bar);
mBtn_03.setImageResource(R.drawable.tab_address_pressed);
break;
case 3:
transaction.show(setting_bar);
mBtn_04.setImageResource(R.drawable.tab_settings_pressed);
break;
default:
break;
}
transaction.commit();
}
//隐藏
private void hideFragment(FragmentTransaction transaction) {
transaction.hide(wexin_bar);
transaction.hide(friend_bar);
transaction.hide(contact_bar);
transaction.hide(setting_bar);
}
//监听点击事件
@Override
public void onClick(View v) {
restImg();
switch (v.getId()){
case R.id.btn_01:
setSelectFragment(0);
break;
case R.id.btn_02:
setSelectFragment(1);
break;
case R.id.btn_03:
setSelectFragment(2);
break;
case R.id.btn_04:
setSelectFragment(3);
default:
break;
}
}
public void restImg(){
mBtn_01.setImageResource(R.drawable.tab_weixin_normal);
mBtn_02.setImageResource(R.drawable.tab_find_frd_normal);
mBtn_03.setImageResource(R.drawable.tab_address_normal);
mBtn_04.setImageResource(R.drawable.tab_settings_normal);
}
public void initEvent(){
mBtn_01.setOnClickListener(this);
mBtn_02.setOnClickListener(this);
mBtn_03.setOnClickListener(this);
mBtn_04.setOnClickListener(this);
}
}
WexinFragment.java
package com.example.wexin;
import android.annotation.TargetApi;
import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class WexinFragment extends Fragment {
public WexinFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//界面对象化
return inflater.inflate(R.layout.wexin_01,container,false);
}
}
其他三个Fragment类似
##最终页面展示
附上我的git:https://github.com/shakeboy/Android-Studio
作者:青春不再弱冠后