• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
SimbaV5
帮助1

主要效果:

学新通

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

学新通

 其中,activity_mail.xml的代码:

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    xmlns:app="http://schemas.android.com/apk/res-auto"
  4.  
    xmlns:tools="http://schemas.android.com/tools"
  5.  
    android:layout_width="match_parent"
  6.  
    android:layout_height="match_parent"
  7.  
    tools:context=".MainActivity">
  8.  
     
  9.  
    <fragment
  10.  
    android:id="@ id/fragment"
  11.  
    android:layout_width="match_parent"
  12.  
    android:layout_height="match_parent"
  13.  
    android:name="com.example.daohang.F_ym1"
  14.  
    />
  15.  
    <TableLayout
  16.  
    android:layout_width="match_parent"
  17.  
    android:layout_height="match_parent"
  18.  
     
  19.  
     
  20.  
    />
  21.  
    <!--增加一个线性横向布局,在页面底部放置功能菜单(共5个按钮)-->
  22.  
    <LinearLayout
  23.  
    android:layout_width="match_parent"
  24.  
    android:layout_height="50dp"
  25.  
    android:orientation="horizontal"
  26.  
    android:layout_alignParentBottom="true"
  27.  
    >
  28.  
    <ImageView
  29.  
    android:id="@ id/imageview1"
  30.  
    android:layout_width="0dp"
  31.  
    android:layout_height="50dp"
  32.  
    android:layout_weight="1"
  33.  
    android:src="@mipmap/tb1"/>
  34.  
    <ImageView
  35.  
    android:id="@ id/imageview2"
  36.  
    android:layout_width="0dp"
  37.  
    android:layout_height="50dp"
  38.  
    android:layout_weight="1"
  39.  
    android:src="@mipmap/tb2"/>
  40.  
    <ImageView
  41.  
    android:id="@ id/imageview3"
  42.  
    android:layout_width="0dp"
  43.  
    android:layout_height="50dp"
  44.  
    android:layout_weight="1"
  45.  
    android:src="@mipmap/tb3"/>
  46.  
    <ImageView
  47.  
    android:id="@ id/imageview4"
  48.  
    android:layout_width="0dp"
  49.  
    android:layout_height="50dp"
  50.  
    android:layout_weight="1"
  51.  
    android:src="@mipmap/tb4"/>
  52.  
    <ImageView
  53.  
    android:id="@ id/imageview5"
  54.  
    android:layout_width="0dp"
  55.  
    android:layout_height="50dp"
  56.  
    android:layout_weight="1"
  57.  
    android:src="@mipmap/tb5"/>
  58.  
    </LinearLayout>
  59.  
     
  60.  
    </RelativeLayout>
学新通

MainActivity的代码:

  1.  
    package com.example.daohang;
  2.  
    import androidx.appcompat.app.AppCompatActivity;
  3.  
    import androidx.fragment.app.Fragment;
  4.  
    import androidx.fragment.app.FragmentManager;
  5.  
    import androidx.fragment.app.FragmentTransaction;
  6.  
    import android.os.Bundle;
  7.  
    import android.view.View;
  8.  
    import android.widget.ImageView;
  9.  
     
  10.  
    public class MainActivity extends AppCompatActivity {
  11.  
     
  12.  
    @Override/**/
  13.  
    protected void onCreate(Bundle savedInstanceState) {
  14.  
    super.onCreate(savedInstanceState);
  15.  
    setContentView(R.layout.activity_main);
  16.  
    /*生成变量绑定控件*/
  17.  
    ImageView iv1=(ImageView) findViewById(R.id.imageview1);
  18.  
    ImageView iv2=(ImageView) findViewById(R.id.imageview2);
  19.  
    ImageView iv3=(ImageView) findViewById(R.id.imageview3);
  20.  
    ImageView iv4=(ImageView) findViewById(R.id.imageview4);
  21.  
    ImageView iv5=(ImageView) findViewById(R.id.imageview5);
  22.  
    /*为变量绑定事件*/
  23.  
    iv1.setOnClickListener(l);
  24.  
    iv2.setOnClickListener(l);
  25.  
    iv3.setOnClickListener(l);
  26.  
    iv4.setOnClickListener(l);
  27.  
    iv5.setOnClickListener(l);
  28.  
    }
  29.  
    /*重写OnClickListener*/
  30.  
    View.OnClickListener l=new View.OnClickListener() {
  31.  
    @Override
  32.  
    /*
  33.  
    * 通过getSupportFragmentManager方法,生成Fragment管理器fm,
  34.  
    * 通过fm的beginTransaction方法,生成Fragment的操作员ft
  35.  
    * 生成Fragment对象f
  36.  
    * 通过判断点击的是哪个imageview,将对应的页面赋值给f
  37.  
    * ft将F插入到主页面的fragment中
  38.  
    * */
  39.  
    public void onClick(View v) {
  40.  
    FragmentManager fm = getSupportFragmentManager();
  41.  
    FragmentTransaction ft=fm.beginTransaction();
  42.  
    Fragment f=null;
  43.  
    switch (v.getId()){
  44.  
    case R.id.imageview1:
  45.  
    f=new F_ym1();
  46.  
    break;
  47.  
    case R.id.imageview2:
  48.  
    f=new F_ym2();
  49.  
    break;
  50.  
    case R.id.imageview3:
  51.  
    f=new F_ym3();
  52.  
    break;
  53.  
    case R.id.imageview4:
  54.  
    f=new F_ym4();
  55.  
    break;
  56.  
    case R.id.imageview5:
  57.  
    f=new F_ym5();
  58.  
    break;
  59.  
    default:
  60.  
    break;
  61.  
    }
  62.  
    ft.replace(R.id.fragment,f);/*ft将F插入到主页面的fragment中*/
  63.  
    ft.commit();/*执行语句*/
  64.  
    }
  65.  
    };
  66.  
    }
学新通

其他参考代码:

  1.  
    <?xml version="1.0" encoding="utf-8"?>
  2.  
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
    android:layout_width="match_parent"
  4.  
    android:layout_height="match_parent">
  5.  
    <TextView
  6.  
    android:layout_width="match_parent"
  7.  
    android:layout_height="match_parent"
  8.  
    android:text="这是页面1"
  9.  
    android:gravity="center"
  10.  
    />
  11.  
     
  12.  
    </RelativeLayout>
  1.  
    package com.example.daohang;
  2.  
     
  3.  
    import android.os.Bundle;
  4.  
    import android.view.LayoutInflater;
  5.  
    import android.view.View;
  6.  
    import android.view.ViewGroup;
  7.  
     
  8.  
    import androidx.annotation.NonNull;
  9.  
    import androidx.annotation.Nullable;
  10.  
    import androidx.fragment.app.Fragment;
  11.  
    /*新建F_ym1类继承Fragment*/
  12.  
    public class F_ym1 extends Fragment {
  13.  
     
  14.  
     
  15.  
    @Nullable
  16.  
    @Override
  17.  
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  18.  
    View view=inflater.inflate(R.layout.f_ym1,null);
  19.  
    return view;
  20.  
    }
  21.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgcjaic
系列文章
更多 icon
同类精品
更多 icon
继续加载