要制作底部导航栏用到了fragment容器,今天先把导航栏部分内容完成,明天我们再进一步完成具体页面
DongtiActivity.java
package com.example.share;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class DongtaiActivity extends FragmentActivity {

private Button btnFaxian, btnDitu, btnFenxiang, btnXiaoxi, btnWode;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dongtai);

    // 找到按钮
    btnFaxian = findViewById(R.id.faxian);
    btnDitu = findViewById(R.id.ditu);
    btnFenxiang = findViewById(R.id.fenxaing);
    btnXiaoxi = findViewById(R.id.xiaoxi);
    btnWode = findViewById(R.id.wode);

    // 设置按钮点击事件
    btnFaxian.setOnClickListener(navClickListener);
    btnDitu.setOnClickListener(navClickListener);
    btnFenxiang.setOnClickListener(navClickListener);
    btnXiaoxi.setOnClickListener(navClickListener);
    btnWode.setOnClickListener(navClickListener);

    // 默认显示发现Fragment
    setFragment(new FaxianFragment());
    updateButtonState(btnFaxian);
}

private View.OnClickListener navClickListener = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Fragment fragment = null;
        int id = v.getId();
        if (id == R.id.faxian) {
            fragment = new FaxianFragment();
            updateButtonState(btnFaxian);
        } else if (id == R.id.ditu) {
            fragment = new DituFragment();
            updateButtonState(btnDitu);
        } else if (id == R.id.fenxaing) {
            fragment = new FenxiangFragment();
            updateButtonState(btnFenxiang);
        } else if (id == R.id.xiaoxi) {
            fragment = new XiaoxiFragment();
            updateButtonState(btnXiaoxi);
        } else if (id == R.id.wode) {
            fragment = new WodeFragment();
            updateButtonState(btnWode);
        }
        if (fragment != null) {
            setFragment(fragment);
        }
    }
};

private void setFragment(Fragment fragment) {
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.fragment_container, fragment);
    fragmentTransaction.commit();
}
private void updateButtonState(Button activeButton) {
    Button[] buttons = {btnFaxian, btnDitu, btnFenxiang, btnXiaoxi, btnWode};
    for (Button button : buttons) {
        button.setBackgroundColor(getResources().getColor(R.color.white));
        button.setTextColor(getResources().getColor(R.color.black));
    }
    activeButton.setBackgroundColor(getResources().getColor(R.color.gray));
    activeButton.setTextColor(getResources().getColor(R.color.white));
}

}
activity_dongtai.xml

<FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/bottom_navigation" />

<RelativeLayout
    android:id="@+id/bottom_navigation"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/white">

    <Button
        android:id="@+id/faxian"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:text="@string/faxian"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:drawableTop="@mipmap/faxian"
        android:drawablePadding="1px"
        android:background="@drawable/button_background" />

    <Button
        android:id="@+id/ditu"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/faxian"
        android:text="@string/ditu"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:drawableTop="@mipmap/ditu"
        android:drawablePadding="1px"
        android:background="@drawable/button_background" />

    <Button
        android:id="@+id/fenxaing"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/ditu"
        android:text="@string/fenxian"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:drawableTop="@mipmap/fabu"
        android:drawablePadding="1px"
        android:background="@drawable/button_background" />

    <Button
        android:id="@+id/xiaoxi"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/fenxaing"
        android:text="@string/xiaoxi"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:drawableTop="@mipmap/xiaoxi"
        android:drawablePadding="1px"
        android:background="@drawable/button_background" />

    <Button
        android:id="@+id/wode"
        android:layout_width="82dp"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/xiaoxi"
        android:text="@string/wode"
        android:textSize="16dp"
        android:textColor="@color/black"
        android:drawableTop="@mipmap/wode"
        android:drawablePadding="1px"
        android:background="@drawable/button_background" />

    <View
        android:id="@+id/divider"
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="@android:color/darker_gray"
        android:layout_above="@id/faxian" />
</RelativeLayout>
posted on 2024-06-28 22:04  一点都不难  阅读(8)  评论(0编辑  收藏  举报