Android学习之--底部菜单FragmentTabHost使用
来源:互联网 发布:医疗器械软件风险管理 编辑:程序博客网 时间:2024/05/27 21:11
记录点滴生活:
一:常见错误:
(一) mytablehost.Fragmentpage1 cannot be cast to android.support.v4.app.Fragment
错误原因:extends Fragment
正确方法:extends android.support.v4.app.Fragment
二:上代码:
步骤一:先写主布局,一个用于显示界面,一个v4.app.FragmentTabHost用于底部栏
(2.1)activity_ main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#fff" android:orientation="vertical"><FrameLayout android:id="@+id/maincontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"></FrameLayout><android.support.v4.app.FragmentTabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/holo_orange_light"> <LinearLayout android:id="@+id/tablecontent" android:layout_width="wrap_content" android:layout_height="wrap_content" > </LinearLayout></android.support.v4.app.FragmentTabHost></LinearLayout>
(2.2) 写3个界面用于显示显示界面的XML布局(fragment_one, two,three),很简单就写一个TextView,
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:textSize="18sp" android:text="First Fragment"/></LinearLayout>
(2.3)写一个名为:tabcontent.XML其作用是写底部菜单栏的布局样式,ImageView,TextView
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" ><ImageView android:id="@+id/table_image" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/table_text" android:textColor="#ff0000"/></LinearLayout>(2.4) 建立Fragmentpage1,2,3 (注意继承是android.support.v4.app.Fragment)
public class Fragmentpage1 extends android.support.v4.app.Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View V=inflater.inflate(R.layout.fragment_one,null); return V; }}
(2.5)MainActivity extens FragmentActivity
public class MainActivity extends FragmentActivity { String name[]={ "首页","第二页","第三页"}; int imgButton[]={android.R.drawable.btn_radio, android.R.drawable.btn_star, android.R.drawable.ic_delete}; Class fragmentArray[]={Fragmentpage1.class,Fragmentpage2.class,Fragmetnpage3.class}; FragmentTabHost fragmentTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); fragmentTabHost= (FragmentTabHost) findViewById(R.id.tabhost); fragmentTabHost.setup(this,getSupportFragmentManager(),R.id.maincontent); /* 3.0因为3.0以下版本是没有fragment的api所以必须借助V4包里面的getSupportFragmentManager()方法来间接获取 FragmentManager()对象。3.0版本之后,有了Fragment的api, 就可以直接使用getFragmentManager()这个方法来获取了*/ for (int i=0;i<name.length;i++){ TabHost.TabSpec tabSpec=fragmentTabHost.newTabSpec(i+"").setIndicator(getView(i)); /*newTabSpec()里参数为一个Tag标签,所以我这里使用i+""加以区别 * 将指示器填充到tabSpec 这个对象中*/ fragmentTabHost.addTab(tabSpec,fragmentArray[i],null); /* .addTab 是将菜单栏按钮和显示界面关联起来*/ } } private View getView(int i){ View view=View.inflate(MainActivity.this,R.layout.tabcontent,null); ImageView imageView= (ImageView) view.findViewById(R.id.table_image); TextView textView= (TextView) view.findViewById(R.id.table_text); imageView.setImageResource(imgButton[i]); textView.setText(name[i]); return view; }}以上都是个人理解,界面丑的一笔,没有任何点击效果,丑的那叫一个惨绝人寰,不敢放图片!
0 0
- Android学习之--底部菜单FragmentTabHost使用
- Android控件使用之FragmentTabHost实现底部菜单
- android 底部菜单写法之FragmentTabHost
- Android底部菜单-FragmentTabHost实现
- Android项目开发实战之使用Fragment和FragmentTabHost搭建底部菜单(一)
- 【Android】FragmentTabHost实现底部Tab菜单选项
- 【Android UI】FragmentTabHost 实现底部菜单
- Android学习之FragmentTabHost配合Fragment使用
- Android中使用FragmentTabHost实现底部导航
- android之FragmentTabHost使用
- Android之底部菜单 BottomNavigationBar的使用
- 用 FragmentTabHost 实现底部菜单
- FragmentTabHost实现底部导航菜单
- 用FragmentTabHost 实现底部菜单
- 利用FragmentTabHost完成底部菜单
- android fragmenttabhost 使用之二
- android fragmenttabhost 使用之三
- Android的FragmentTabHost使用总结(顶部或底部菜单栏)
- Freemarker常用技巧-1
- app中icon的设置
- Android7.0新特性
- php内置函数number_format
- Java虚拟机内存管理(一)
- Android学习之--底部菜单FragmentTabHost使用
- 将博客搬至CSDN
- AWS更改root密码
- 不难不易的js加密
- strstr、strspn如何使用
- qt 手动设置控件的位置
- MySQL系列第二篇:MySQL可视化工具Navicat for MySQL安装配置和使用
- 大型核心系统的升级或迁移
- Android Parcelable和Serializable的区别