界面分为三块
最上面显示标题
中间显示文字内容
最下面显示菜单
其中每个界面进行切换的时候,除了中间的文字内容会变化,下方的菜单颜色也会由于选中的不同而变色
bottom部分的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="90dp"
android:background="@color/black">
<LinearLayout
android:id="@+id/LinearLayout_weixin"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/weixin1" />
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="消息"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout_friend"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/shezhi1" />
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="设置"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout_xinxi"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/faxian1" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="发现"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/LinearLayout_setting"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/haoyou1" />
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="好友"
android:textColor="@color/white"
android:textSize="30sp" />
</LinearLayout>
</LinearLayout>
主文件代码
package com.example.mywork_l;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Fragment weixinFragment=new weixinFragment();
private Fragment friendFragment=new friendFragment();
private Fragment configFragment=new configFragment();
private Fragment contactFragment=new contactFragment();
private FragmentManager fragmentManager;
private LinearLayout linearLayout1,linearLayout2,linearLayout3,linearLayout4;
private ImageView imageView1,imageView2,imageView3,imageView4;
private TextView textView1,textView2,textView3,textView4;
@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_friend);
linearLayout3=findViewById(R.id.LinearLayout_xinxi);
linearLayout4=findViewById(R.id.LinearLayout_setting);
linearLayout1.setOnClickListener(this);
linearLayout2.setOnClickListener(this);
linearLayout3.setOnClickListener(this);
linearLayout4.setOnClickListener(this);
imageView1=findViewById(R.id.imageView1);
imageView2=findViewById(R.id.imageView2);
imageView3=findViewById(R.id.imageView3);
imageView4=findViewById(R.id.imageView4);
textView1=findViewById(R.id.textView1);
textView2=findViewById(R.id.textView2);
textView3=findViewById(R.id.textView3);
textView4=findViewById(R.id.textView4);
initFragment();
}
private void initFragment(){
fragmentManager=getFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.add(R.id.id_content,weixinFragment);
transaction.add(R.id.id_content,friendFragment);
transaction.add(R.id.id_content,contactFragment);
transaction.add(R.id.id_content,configFragment);
hideFragment(transaction);
transaction.commit();
}
private void hideFragment(FragmentTransaction transaction){
transaction.hide(weixinFragment);
transaction.hide(friendFragment);
transaction.hide(contactFragment);
transaction.hide(configFragment);
}
public void onClick(View v){
switch (v.getId()){
case R.id.LinearLayout_weixin:
changeColor(0);
showfragment(0);
break;
case R.id.LinearLayout_friend:
changeColor(1);
showfragment(1);
break;
case R.id.LinearLayout_xinxi:
changeColor(2);
showfragment(2);
break;
case R.id.LinearLayout_setting:
changeColor(3);
showfragment(3);
break;
default:
break;
}
}
private void changeColor(int i) {
imageView1.setImageResource(R.drawable.weixin1);
imageView2.setImageResource(R.drawable.shezhi1);
imageView3.setImageResource(R.drawable.faxian1);
imageView4.setImageResource(R.drawable.haoyou1);
textView1.setTextColor(Color.rgb(255, 255, 255));
textView2.setTextColor(Color.rgb(255, 255, 255));
textView3.setTextColor(Color.rgb(255, 255, 255));
textView4.setTextColor(Color.rgb(255, 255, 255));
switch (i){
case 0:
imageView1.setImageResource(R.drawable.weixin2);
textView1.setTextColor(Color.rgb(255,0,0));
break;
case 1:
imageView2.setImageResource(R.drawable.shezhi2);
textView2.setTextColor(Color.rgb(255,0,0));
break;
case 2:
imageView3.setImageResource(R.drawable.faxian2);
textView3.setTextColor(Color.rgb(255,0,0));
break;
case 3:
imageView4.setImageResource(R.drawable.haoyou2);
textView4.setTextColor(Color.rgb(255,0,0));
break;
default:
break;
}
}
private void showfragment(int i) {
FragmentTransaction transaction=fragmentManager.beginTransaction();
hideFragment(transaction);
switch (i){
case 0:
transaction.show(weixinFragment);
break;
case 1:
transaction.show(configFragment);
break;
case 2:
transaction.show(contactFragment);
break;
case 3:
transaction.show(friendFragment);
break;
default:
break;
}
transaction.commit();
}
}
源码仓库地址