Mainctivity
package com.example.a01380913.ccss; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { LinearLayout line1,line2,line3,line4; TextView text1,text2,text3,text4; ImageView image1,image2,image3,image4; private F1 f1; private F2 f2; private F3 f3; private F4 f4; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); line1=findViewById(R.id.line1); line2=findViewById(R.id.line2); line3=findViewById(R.id.line3); line4=findViewById(R.id.line4); text1=findViewById(R.id.text1); text2=findViewById(R.id.text2); text3=findViewById(R.id.text3); text4=findViewById(R.id.text4); image1=findViewById(R.id.image1); image2=findViewById(R.id.image2); image3=findViewById(R.id.image3); image4=findViewById(R.id.image4); f1 = new F1(); f2 = new F2(); f3 = new F3(); f4 = new F4(); getSupportFragmentManager().beginTransaction().replace(R.id.bj,f1).commit(); text1.setTextColor(Color.RED); text2.setTextColor(Color.BLACK); text3.setTextColor(Color.BLACK); text4.setTextColor(Color.BLACK); image1.setImageResource(R.mipmap.icon_choose_light); image2.setImageResource(R.mipmap.ic_launcher); image3.setImageResource(R.mipmap.ic_launcher); image4.setImageResource(R.mipmap.ic_launcher); line1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction().replace(R.id.bj,f1).commit(); text1.setTextColor(Color.RED); text2.setTextColor(Color.BLACK); text3.setTextColor(Color.BLACK); text4.setTextColor(Color.BLACK); image1.setImageResource(R.mipmap.icon_choose_light); image2.setImageResource(R.mipmap.ic_launcher); image3.setImageResource(R.mipmap.ic_launcher); image4.setImageResource(R.mipmap.ic_launcher); } }); line2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction().replace(R.id.bj,f2).commit(); text2.setTextColor(Color.RED); text1.setTextColor(Color.BLACK); text3.setTextColor(Color.BLACK); text4.setTextColor(Color.BLACK); image2.setImageResource(R.mipmap.icon_choose_light); image1.setImageResource(R.mipmap.ic_launcher); image3.setImageResource(R.mipmap.ic_launcher); image4.setImageResource(R.mipmap.ic_launcher); } }); line3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction().replace(R.id.bj,f3).commit(); text3.setTextColor(Color.RED); text2.setTextColor(Color.BLACK); text1.setTextColor(Color.BLACK); text4.setTextColor(Color.BLACK); image3.setImageResource(R.mipmap.icon_choose_light); image2.setImageResource(R.mipmap.ic_launcher); image1.setImageResource(R.mipmap.ic_launcher); image4.setImageResource(R.mipmap.ic_launcher); } }); line4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getSupportFragmentManager().beginTransaction().replace(R.id.bj,f4).commit(); text4.setTextColor(Color.RED); text2.setTextColor(Color.BLACK); text3.setTextColor(Color.BLACK); text1.setTextColor(Color.BLACK); image4.setImageResource(R.mipmap.icon_choose_light); image2.setImageResource(R.mipmap.ic_launcher); image3.setImageResource(R.mipmap.ic_launcher); image1.setImageResource(R.mipmap.ic_launcher); } }); } }
———————————————————————————————————————————————————————
xml
<?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="match_parent" android:orientation="vertical" tools:context=".Main2Activity"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:id="@+id/bj" ></FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="8" > <LinearLayout android:id="@+id/line1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:text="首页" android:textColor="#000" android:gravity="center" /> </LinearLayout> <LinearLayout android:id="@+id/line2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:text="分类" android:textColor="#000" android:gravity="center" /> </LinearLayout> <LinearLayout android:id="@+id/line3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text3" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:text="购物车" android:textColor="#000" android:gravity="center" /> </LinearLayout> <LinearLayout android:id="@+id/line4" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:layout_marginTop="5dp" > <ImageView android:id="@+id/image4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:src="@mipmap/ic_launcher" /> <TextView android:id="@+id/text4" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="2" android:text="我的" android:textColor="#000" android:gravity="center" /> </LinearLayout> </LinearLayout> </LinearLayout>