Android Studio 入门教程底部导航按钮+Fragment切换
主要效果:
主要文件:(最主要的也就activity_mail.xml和MainActivity两个)
其中,activity_mail.xml的代码:
-
-
<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 {
-
-
/**/
-
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() {
-
-
/*
-
* 通过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();/*执行语句*/
-
}
-
};
-
}
其他参考代码:
-
-
<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 {
-
-
-
-
-
public View onCreateView( { LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
-
View view=inflater.inflate(R.layout.f_ym1,null);
-
return view;
-
}
-
}
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgcjaic
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01