FragmentTabHost是目前主流的底部菜单使用的方法,但是有开源项目实现的更加美观,使用起来也更加方便简单。但是作为初学者,我们还是以归纳知识为
主要目的进行系统的学习
FragmentTabHost比之TabHost更加稳定,更加简单流畅。
首先是底部菜单的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center_horizontal" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_height="wrap_content" android:layout_width="wrap_content" /> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="20dp" android:padding="20dp" android:textColor="@android:color/white"/> </LinearLayout>
其次是主页面的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <!-- 存放主要页面内容 --> <FrameLayout android:id="@+id/maincontent" android:layout_width="fill_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> <!-- 底层菜单 --> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#4F4F4F" > <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0" > </FrameLayout> </android.support.v4.app.FragmentTabHost> </LinearLayout>
注意这里的命名规则,不能随意更改其命名。最后是主文件;
package com.example.day04_fragmenttabhost; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTabHost; import android.view.View; import android.widget.ImageView; import android.widget.TabHost.TabSpec; import android.widget.TextView; public class MainActivity extends FragmentActivity { private FragmentTabHost fragmentTabHost; private String texts[] = { "首页", "消息", "好友", "广场" }; //添加按钮背景图片资源 // private int imageButton[] = { R.drawable.bt_home_selector, // R.drawable.bt_message_selector, R.drawable.bt_selfinfo_selector,R.drawable.bt_square_selector ,R.drawable.bt_more_selector}; private Class fragmentArray[] = {FragmentOne.class,FragmentTwo.class,FragmentThree.class,FragmentFore.class}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 实例化tabhost fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); fragmentTabHost.setup(this, getSupportFragmentManager(), R.id.maincontent); for (int i = 0; i < texts.length; i++) { TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i)); fragmentTabHost.addTab(spec, fragmentArray[i], null); //设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常) // fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector); } } private View getView(int i) { //取得布局实例 View view=View.inflate(MainActivity.this, R.layout.tabcontent, null); //取得布局对象 ImageView imageView=(ImageView) view.findViewById(R.id.image); TextView textView=(TextView) view.findViewById(R.id.text); //设置图标 // imageView.setImageResource(imageButton[i]); //设置标题 textView.setText(texts[i]); return view; } }
到了这里,也学习了不少的布局方式,其大致思路均是:实例化对象,设置数据源,选择适配器,绑定适配器,最后实现所要的功能。适配器这里有很多记忆的东西,望以后自己复习的时候能多注重这方面的记忆。
本文参考:http://www.cnblogs.com/lichenwei/p/3985121.html 这个大神UI方面写了很多漂亮实用的东西,没事可以去逛逛