底部导航栏:FragmentTabHost+Fragment

来源:互联网 发布:知礼仪 明事理 辨是非 编辑:程序博客网 时间:2024/05/22 00:06
原创出处:http://blog.csdn.net/yangyu20121224/article/details/9016223
1、布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    >    <FrameLayout        android:id="@+id/<span style="color: rgb(255, 0, 0);">realcontent</span>"        android:layout_width="match_parent"        android:layout_height="0dp"        android:layout_weight="1"       />    <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"/>        </android.support.v4.app.FragmentTabHost></LinearLayout>
2、导航栏中图片的xml布局文件:(点击图片变色)另外3个类似
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:state_selected="true" android:drawable="@drawable/tabbar_home_selected"/><!--选中状态-->    <item android:state_window_focused="false" android:drawable="@drawable/tabbar_home" /><!--未选中状态--></selector>
3、导航栏中文字的xml布局文件:(点击文字变色)在textview中添加: android:textColor="@drawable/tabhost_text"
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android" >    <item android:state_selected="true" android:color="#0082FF"></item>    <item android:state_selected="false" android:color="#000000"></item></selector>
4、导航栏单个item的布局文件:
<?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/ivItem"        android:layout_width="30dp"        android:layout_height="30dp"        android:layout_gravity="center"        />    <TextView        android:id="@+id/tvItem"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:textColor="@drawable/tabhost_text"        /></LinearLayout>
5、主Activity:public class MainActivity extends FragmentActivity {    private FragmentTabHost mTabHost;
    //导航栏item对应的Fragment:
    private Class[] mFragemntArrays = {Home.class,Message.class, Find.class,User.class};
    //导航栏item图片对应的背景文件:    private int[] mImageArrays = {R.drawable.tabhost_home,R.drawable.tabhost_message,R.drawable.tabhost_find,R.drawable.tabhost_user}; //    private String[] mTextArrays={"首页","消息","发现","我"};    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        initTabHost();//fragmenttabhost初始化    }    private void initTabHost() {        //实例化        mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);        mTabHost.setup(this, getSupportFragmentManager(),<span style="color:#ff0000;">R.id.realcontent</span>);        int count = mFragemntArrays.length;        for (int i=0;i<count;i++){            TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextArrays[i]).setIndicator(getTabItemView(i));            mTabHost.addTab(tabSpec,mFragemntArrays[i],null);            //设置没有分割线            mTabHost.getTabWidget().setDividerDrawable(null);        }    }    //导航栏item设置图片和文字    private View getTabItemView(int i) {        View view_item = LayoutInflater.from(this).inflate(R.layout.tabhost_item, null);        ImageView imageView = (ImageView) view_item.findViewById(R.id.ivItem);        TextView textView = (TextView) view_item.findViewById(R.id.tvItem);        imageView.setBackgroundResource(mImageArrays[i]);        textView.setText(mTextArrays[i]);        return view_item;    }}

6、导航栏item对应的Fragment:
public class Message extends Fragment {    private View view;    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {        view = inflater.inflate(R.layout.message,null);        return view;    }}


                                             
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 洗衣服时衣服粘上卫生纸怎么办 一晚上卫生巾都是满的怎么办 宝宝头上痱子痒怎么办 短裤里的宽松紧带拧了怎么办 肉色内衣被染黑色了怎么办 安全裤总往上缩怎么办 夏天穿裙子膝盖怕凉怎么办 夏天穿裙子膝盖冷怎么办 天凉嗓子痒咳嗽怎么办 棉服里面跑毛怎么办 棉衣里面的棉一块一块的怎么办 穿姨妈巾悟出痱子怎么办 穿裙子上衣太长了怎么办 微信封号了零钱怎么办 快递加盟商欠我工资怎么办 加盟费交了以后怎么办 加盟总部违约加盟商该怎么办 自行车锁钥匙丢了怎么办 假体隆胸肿胀痛怎么办 恶露60天不干净怎么办 剖腹产俩月恶露不干净怎么办 剖腹产恶露一个多月还没干净怎么办 小月子全身流虚汗怎么办 生完孩子严重便秘怎么办啊 产后第5天恶露少怎么办 有恶露排不出来怎么办 刚生完小孩便秘怎么办 产后15天有鲜血怎么办 恶露未净同房了怎么办 药流2月恶露不尽怎么办 生完孩子小腹痛怎么办 月子里恶露有异味怎么办 恶露干净了有异味怎么办 顺产后恶露白色异味怎么办 产妇排出的恶露有异味怎么办 恶露有股腥臭味怎么办 生完孩子下面有异味怎么办 销售货物开票税率错了怎么办 8h床垫有橡胶味怎么办 买回来的鞋子臭怎么办 耐克保修卡没了怎么办