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