Fragment底部导航

布局代码

<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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值