- 一、运行效果图
- 二、top.xml布局及代码
- 三、bottom.xml布局及代码
- 四、activity_main.xml布局及代码
- 五、MainActivity代码
- 六、页面部分
- 1、fragment_weixin
- 2、fragment_contact
- 3、fragment_discover
- 4、fragment_config
- 其他Java部分
- 1、weixinFragment
- 2、contactFragment
- 3、discoverFragment
- 4、configFragment
- gitee仓库链接
三、bottom.xml布局及代码
四、activity_main.xml布局及代码
五、MainActivity代码
package com.example.myapplication;
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.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
//创建对象
private Fragment weixinFragment=new weixinFragment();
private Fragment contactFragment=new contactFragment();
private Fragment discoverFragment=new discoverFragment();
private Fragment configFragment=new configFragment();
private FragmentManager fragmentManager;
private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
private ImageButton mImgweixin;
private ImageButton mImgcontact;
private ImageButton mImgdiscover;
private ImageButton mImgconfig;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
linearLayout1=findViewById(R.id.LinearLayout_weixin);
linearLayout2=findViewById(R.id.LinearLayout_contact);
linearLayout3=findViewById(R.id.LinearLayout_discover);
linearLayout4=findViewById(R.id.LinearLayout_config);
mImgweixin = findViewById(R.id.imageButton_weixin);
mImgcontact = findViewById(R.id.imageButton_contact);
mImgdiscover = findViewById(R.id.imageButton_discover);
mImgconfig = findViewById(R.id.imageButton_config);
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
initFragment();
selectFragment(0);
}
private void initFragment(){
fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.id_content,weixinFragment);
transaction.add(R.id.id_content,contactFragment);
transaction.add(R.id.id_content,discoverFragment);
transaction.add(R.id.id_content,configFragment);
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction){
transaction.hide(weixinFragment);
transaction.hide(contactFragment);
transaction.hide(discoverFragment);
transaction.hide(configFragment);
}
@Override
public void onClick(View v) {
resetImgs();
switch (v.getId()){
case R.id.LinearLayout_weixin:
selectFragment(0);
break;
case R.id.LinearLayout_contact:
selectFragment(1);
break;
case R.id.LinearLayout_discover:
selectFragment(2);
break;
case R.id.LinearLayout_config:
selectFragment(3);
break;
default:
break;
}
}
private void selectFragment(int i){
FragmentTransaction transaction=fragmentManager.beginTransaction();
hideFragment(transaction);
switch (i){
case 0:
transaction.show(weixinFragment);
mImgweixin.setImageResource(R.drawable.weixin_pressed);
break;
case 1:
transaction.show(contactFragment);
mImgcontact.setImageResource(R.drawable.contact_pressed);
break;
case 2:
transaction.show(discoverFragment);
mImgdiscover.setImageResource(R.drawable.discover_pressed);
break;
case 3:
transaction.show(configFragment);
mImgconfig.setImageResource(R.drawable.config_pressed);
break;
default:
break;
}
transaction.commit();
}
//提供灰暗图片
private void resetImgs() {
mImgweixin.setImageResource(R.drawable.weixin);
mImgcontact.setImageResource(R.drawable.contact);
mImgdiscover.setImageResource(R.drawable.discover);
mImgconfig.setImageResource(R.drawable.config);
}
}
六、页面部分
1、fragment_weixin
2、fragment_contact
3、fragment_discover
4、fragment_config
其他Java部分 1、weixinFragment
package com.example.myapplication;
import android.os.Bundle;
import android.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class weixinFragment extends Fragment {
public weixinFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_weixin, container, false);
}
}
2、contactFragment
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class contactFragment extends Fragment {
public contactFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_contact, container, false);
}
}
3、discoverFragment
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class discoverFragment extends Fragment {
public discoverFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_discover, container, false);
}
}
4、configFragment
package com.example.myapplication;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.app.Fragment;
public class configFragment extends Fragment {
public configFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_config, container, false);
}
}
gitee仓库链接


