布局代码
<RelativeLayout
android:id="@+id/top"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@color/redtitle">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:text="首页"
android:textColor="#fff"
android:textSize="18sp" />
</RelativeLayout>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RelativeLayout
android:id="@+id/rel_home"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/im_home"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="@drawable/main_home" />
<TextView
android:id="@+id/tv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/im_home"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="首页"
android:textSize="14sp" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/rel_my"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/im_my"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:src="@drawable/mine_gray" />
<TextView
android:id="@+id/tv_my"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/im_my"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="我的"
android:textSize="14sp" />
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/bottom"
android:layout_below="@+id/top"></FrameLayout>
首页、我的 java代码
public class MainActivity_Home extends Fragment {
private View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_home, container, false);
return view;
}
}
核心代码
private RelativeLayout rel_home,rel_my;
private ImageView im_home,im_my;
private TextView tv_home,tv_my,title;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findIDs();
setListener();
efor(0);
showFrame(new MainActivity_My());
}
private void findIDs() {
rel_home=(RelativeLayout)findViewById(R.id.rel_home);
rel_my=(RelativeLayout)findViewById(R.id.rel_my);
im_home=(ImageView)findViewById(R.id.im_home);
im_my=(ImageView)findViewById(R.id.im_my);
tv_home=(TextView)findViewById(R.id.tv_home);
tv_my=(TextView)findViewById(R.id.tv_my);
title=(TextView)findViewById(R.id.title);
}
private void setListener() {
rel_home.setOnClickListener(this);
rel_my.setOnClickListener(this);
}
private void showFrame(Fragment fragment) {
FragmentManager fragmentManager =getSupportFragmentManager();
FragmentTransaction manager = fragmentManager.beginTransaction();
manager.replace(R.id.framelayout,fragment);
manager.commit();
}
private void efor(int index) {
if (index == 0){
im_home.setImageResource(R.drawable.main_redhome);
tv_home.setTextColor(Color.parseColor("#DE413F"));
im_my.setImageResource(R.drawable.mine_gray);
tv_my.setTextColor(Color.parseColor("#454545"));
title.setText("首页");
}else if (index==1){
im_home.setImageResource(R.drawable.main_home);
tv_home.setTextColor(Color.parseColor("#454545"));
im_my.setImageResource(R.drawable.mine_red);
tv_my.setTextColor(Color.parseColor("#DE413F"));
title.setText("我的");
}
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.rel_home:
efor(0);
showFrame(new MainActivity_Home());
break;
case R.id.rel_my:
efor(1);
showFrame(new MainActivity_My());
break;
}
}