版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中利用fragment实现一个底部标题栏
本篇文章为大家展示了怎么在Android中利用fragment实现一个底部标题栏,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
fragment特点Fragment与Activity相似,有自己的生命周期,布局。相当于一个迷你的ActivityFragment可以作为Activity的组成部分,一个Activity可以有多个Fragment一个Fragment可以被多个Activity重用在Activity运行时可动态地加入、移除、交换Fragment一个具有自己生命周期的控件,有自己的处理输入事件的能力依赖于Activity,能互相通信和托管。一.activity_main.xml<?xml
version="1.0"
encoding="utf-8"?>
<RelativeLayout
xmlns:android="/apk/res/android"
xmlns:app="/apk/res-auto"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/tab_linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="@color/colorPrimary">
<LinearLayout
android:id="@+id/home"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp">
<ImageView
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/home"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="主页"
android:textColor="@drawable/text_color_back"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/location"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp">
<ImageView
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/location_view"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="地点"
android:textColor="@drawable/text_color_back"
/>
</LinearLayout>
<LinearLayout
android:id="@+id/linear_polymer"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp">
<ImageView
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/comment"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="聊天"
android:textColor="@drawable/text_color_back"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:id="@+id/linear_user"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="60dp">
<ImageView
android:layout_gravity="center"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/contrast_view"
/>
<TextView
android:layout_gravity="center"
android:text="设置"
android:textColor="@drawable/text_color_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<FrameLayout
android:id="@+id/fragment_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/tab_linear">
</FrameLayout>
</RelativeLayout>编写好的界面如下:
然后在我们最开始的演示视频当中大家也看到了我们每点击一次按钮,按钮的颜色就会发生变化,因此我们需要为每一个按钮编写选择器selector,这里就只展示第一个选择器"主页"的selector吧,还有三个按钮,咱们可以利用同样的方式建立selector,如果想要了解其他按钮的selector编写的话,请前往github:/Geeksongs/ButtonTitile二.home.xml<?xml
version="1.0"
encoding="utf-8"?>
<selector
xmlns:android="/apk/res/android">
<item
android:state_selected="true"
android:drawable="@drawable/home3"/>
<item
android:drawable="@drawable/home31"/>
</selector>其中上面的图片我均放置在了drawble文件夹当中,这里强烈推荐阿里云矢量图标库,在这里可以找到你想要图标,网址如下:/。然后找到你所需要的图标之后就可以进行下载啦!三.fragment1.java接下来是对碎片fragment1.java代码的编写,在这段代码的编写当中所需要注意的是我们将会返回整个fragment.xml的view布局,而不是直接返回一个textview或者imageview之类的控件,这样会让初学者感到十分困惑,为什么不返回整个fragment所对应的xml界面,代码如下:import
android.os.Bundle;
import
androidx.annotation.Nullable;
import
androidx.fragment.app.Fragment;
import
android.view.LayoutInflater;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.TextView;
/**
*
A
simple
{@link
Fragment}
subclass.
*/
public
class
Fragment1
extends
Fragment
{
private
String
fragmentText;
private
TextView
fragmentTextView;
@Nullable
@Override
public
View
onCreateView(LayoutInflater
inflater,
@Nullable
ViewGroup
container,
@Nullable
Bundle
savedInstanceState)
{
View
view=inflater.inflate(R.layout.fragment_fragment1,container,false);
return
view;//返回view布局
}
public
Fragment1(String
fragmentText)
{
this.fragmentText=fragmentText;
}
}其余几个fragment的代码也差不多,只是其构造方法的名称略有不同,所使用了fragment1(2/3/4),毕竟它们的类名不同嘛。编写了fragment的Java代码,是时候编写fragment的xml代码了,因为这样才可以将编写好的界面传递到主界面:activity_main.xml当中,代码如下:四.fragment1.xml<?xml
version="1.0"
encoding="utf-8"?>
<FrameLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Fragment1">
<!--
TODO:
Update
blank
fragment
layout
-->
<TextView
android:id="@+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30dp"
android:text="这是第一个碎片"
/>
</FrameLayout>由于安卓默认的字体比较小,我就略微修改了一下将字体的大小修改为了30dp,当然你也可以根据自己的需要进行改动,这个fragment文件我们一共需要建立4份,毕竟有四个底部标题栏的按钮。五.MainActivity.java下面是主活动的Java代码:public
class
MainActivity
extends
AppCompatActivity
implements
View.OnClickListener{
LinearLayout
homeLinear;
LinearLayout
listLinear;
LinearLayout
polyLinear;
LinearLayout
userLinear;
Fragment1
fragmentHome;
Fragment2
fragmentList;
Fragment3
fragmentPoly;
Fragment4
fragmentUser;
private
FragmentManager
mfragmentManger;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
homeLinear=
(LinearLayout)
findViewById(R.id.home);
listLinear=
(LinearLayout)
findViewById(R.id.location);
polyLinear=
(LinearLayout)
findViewById(R.id.linear_polymer);
userLinear=
(LinearLayout)
findViewById(R.id.linear_user);
homeLinear.setOnClickListener(this);
listLinear.setOnClickListener(this);
polyLinear.setOnClickListener(this);
userLinear.setOnClickListener(this);
mfragmentManger
=
getSupportFragmentManager();
homeLinear.performClick();
}
@Override
public
void
onClick(View
view)
{
FragmentTransaction
fragmentTransaction
=
mfragmentManger.beginTransaction();//只能是局部变量,不能为全局变量,否则不能重复commit
//FragmentTransaction只能使用一次
hideAllFragment(fragmentTransaction);
switch
(view.getId()){
case
R.id.home:
setAllFalse();
homeLinear.setSelected(true);
if
(fragmentHome==null){
fragmentHome=new
Fragment1("Home");
fragmentTransaction.add(R.id.fragment_frame,fragmentHome);
}else{
fragmentTransaction.show(fragmentHome);
}
break;
case
R.id.location:
setAllFalse();
listLinear.setSelected(true);
if(fragmentList==null){
fragmentList=new
Fragment2("List");
fragmentTransaction.add(R.id.fragment_frame,fragmentList);
}else
{
fragmentTransaction.show(fragmentList);
}
break;
case
R.id.linear_polymer:
setAllFalse();
polyLinear.setSelected(true);
if(fragmentPoly==null){
fragmentPoly=new
Fragment3("Polymer");
fragmentTransaction.add(R.id.fragment_frame,fragmentPoly);
}else
{
fragmentTransaction.show(fragmentPoly);
}
break;
case
R.id.linear_user:
setAllFalse();
userL
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 工程项目管理培训学习体会
- 农业创新科技:病虫害防治方案
- 糖尿病夏季:防晒避暑与防范
- 公司员工聘用协议-行政专员
- 健康规划-高血压的干预方案
- 糖尿病与法律:权益保障指南
- 体育场馆管理:第六章综合管理要点
- 老年抑郁症的临床护理分析
- 三方抵押借款合同
- 城镇排水设施建设、运维管理办法
- 2024-2029年中国电线电缆行业市场深度分析及竞争格局与投资研究报告
- 筑牢安全生产防线,保障
- 江苏省交通运输厅直属事业单位2024年公开招聘历年高频考题难、易错点模拟试题(共500题)附带答案详解
- 先进制造技术智慧树知到期末考试答案2024年
- 绝缘子行业发展形势分析报告
- 广东省深圳市2023-2024学年四年级下册第1-4单元期中模拟测试数学试卷(北师大版)
- 2023-2024学年广东省深圳市十校联考中考三模物理试题含解析
- 新能源产业发展与安全应用白皮书
- 2012-2021年广东省历年中考数学试题分类及答案解析(近10年)
- 中医祛湿养生方法的课件
- (高清版)DZT 0073-2016 电阻率剖面法技术规程
评论
0/150
提交评论