Android Studio 入门教程:底部导航按钮+Fragment切换

主要效果:

 主要文件:(最主要的也就activity_mail.xml和MainActivity两个)

 其中,activity_mail.xml的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".MainActivity">

    <fragment
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.daohang.F_ym1"
        />
    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"


        />
    <!--增加一个线性横向布局,在页面底部放置功能菜单(共5个按钮)-->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal"
        android:layout_alignParentBottom="true"
        >
        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/tb1"/>
        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/tb2"/>
        <ImageView
            android:id="@+id/imageview3"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/tb3"/>
        <ImageView
            android:id="@+id/imageview4"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/tb4"/>
        <ImageView
            android:id="@+id/imageview5"
            android:layout_width="0dp"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:src="@mipmap/tb5"/>
    </LinearLayout>

</RelativeLayout>

MainActivity的代码:

package com.example.daohang;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override/**/
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        /*生成变量绑定控件*/
        ImageView iv1=(ImageView) findViewById(R.id.imageview1);
        ImageView iv2=(ImageView) findViewById(R.id.imageview2);
        ImageView iv3=(ImageView) findViewById(R.id.imageview3);
        ImageView iv4=(ImageView) findViewById(R.id.imageview4);
        ImageView iv5=(ImageView) findViewById(R.id.imageview5);
        /*为变量绑定事件*/
        iv1.setOnClickListener(l);
        iv2.setOnClickListener(l);
        iv3.setOnClickListener(l);
        iv4.setOnClickListener(l);
        iv5.setOnClickListener(l);
    }
    /*重写OnClickListener*/
    View.OnClickListener l=new View.OnClickListener() {
        @Override
        /*
        * 通过getSupportFragmentManager方法,生成Fragment管理器fm,
        * 通过fm的beginTransaction方法,生成Fragment的操作员ft
        * 生成Fragment对象f
        * 通过判断点击的是哪个imageview,将对应的页面赋值给f
        * ft将F插入到主页面的fragment中
        * */
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft=fm.beginTransaction();
            Fragment f=null;
            switch (v.getId()){
                case R.id.imageview1:
                    f=new F_ym1();
                    break;
                case R.id.imageview2:
                    f=new F_ym2();
                    break;
                case R.id.imageview3:
                    f=new F_ym3();
                    break;
                case R.id.imageview4:
                    f=new F_ym4();
                    break;
                case R.id.imageview5:
                    f=new F_ym5();
                    break;
                default:
                    break;
            }
            ft.replace(R.id.fragment,f);/*ft将F插入到主页面的fragment中*/
            ft.commit();/*执行语句*/
        }
    };
}

其他参考代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="这是页面1"
        android:gravity="center"
        />

</RelativeLayout>
package com.example.daohang;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
/*新建F_ym1类继承Fragment*/
public class F_ym1 extends Fragment {


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.f_ym1,null);
        return view;
    }
}

  • 2
    点赞
  • 42
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
uni-app是一个跨平台的开发框架,可以用于开发多个平台的应用程序。在uni-app中,可以通过自定义底部导航栏来实现个性化的界面设计和功能需求。 以下是一种实现自定义底部导航栏的方法: 1. 首先,创建一个自定义组件,用于显示底部导航栏的每个按钮。可以使用`uni-icons`组件来显示图标,并添加相应的点击事件。 ```vue <template> <view> <view class="tabbar"> <view class="tabbar-item" @click="navigateTo('home')"> <uni-icons type="home"></uni-icons> <text>首页</text> </view> <view class="tabbar-item" @click="navigateTo('discover')"> <uni-icons type="discover"></uni-icons> <text>发现</text> </view> <view class="tabbar-item" @click="navigateTo('mine')"> <uni-icons type="mine"></uni-icons> <text>我的</text> </view> </view> </view> </template> <script> export default { methods: { navigateTo(page) { uni.navigateTo({ url: `/pages/${page}/${page}` }); } } } </script> <style> .tabbar { display: flex; justify-content: space-around; align-items: center; height: 100px; background-color: #f5f5f5; } .tabbar-item { display: flex; flex-direction: column; align-items: center; } </style> ``` 2. 在需要显示底部导航栏的页面中,引入自定义组件,并在页面底部位置使用该组件。 ```vue <template> <view> <!-- 页面内容 --> </view> <custom-tabbar></custom-tabbar> </template> <script> import CustomTabbar from '@/components/CustomTabbar.vue'; export default { components: { CustomTabbar } } </script> ``` 通过以上步骤,你可以实现一个自定义的底部导航栏,并在不同的页面之间进行切换

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值