FragmentTabHost的使用

1.先来看下页面的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >


    <FrameLayout
        android:id="@+id/realtabcontent"
        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:orientation="horizontal" />
    </android.support.v4.app.FragmentTabHost>


</LinearLayout>

在布局的时候有一点要记住上面的FragmentTabHost的id要记住是android的id“"@android:id/tabhost"”

2.底部按钮变色:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/tab_address_pressed" android:state_focused="true"/>
    <item android:drawable="@drawable/tab_address_pressed" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_address_normal"/>
</selector>

处理被选中的时候跟得到焦点的时候

3.xml基本上到这边了接下来看下代码

package com.example.weixin;


import java.util.ArrayList;
import java.util.List;


import Bean.Tab;
import Fragmentactivity.FragmenMessage;
import Fragmentactivity.Fragmen_personage;
import Fragmentactivity.Fragment_home;
import Fragmentactivity.Fragment_hot;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
/**
 * @author chen
 *
 */
public class MainActivity extends FragmentActivity {
    private FragmentTabHost mTabHost=null;
    private LayoutInflater mInflater=null;
    private List<Tab> mtabs=new ArrayList<Tab>();
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
    
private void init() 
{
mTabHost=(FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);  
Tab  home=new Tab(R.string.home, R.drawable.homeselector,Fragment_home.class);
Tab  hot=new Tab(R.string.hot,R.drawable.hotselector,Fragment_hot.class);
Tab  personage=new Tab(R.string.personal, R.drawable.personalselector, Fragmen_personage.class);
Tab  message=new Tab(R.string.message, R.drawable.mseeageselector,FragmenMessage.class);
mtabs.add(home);
mtabs.add(hot);
mtabs.add(personage);
mtabs.add(message);
mInflater=LayoutInflater.from(this);
for(Tab tab:mtabs)
{
TabHost.TabSpec tabSpec=mTabHost.newTabSpec(getString(tab.getTitle()));
tabSpec.setIndicator(buildView(tab));
mTabHost.addTab(tabSpec,tab.getFragment(),null);
}
}
/**
* 创建tab视图
* @param tab
* @return
*/
private View buildView(Tab tab)
{
View view=mInflater.inflate(R.layout.tbahost_view, null);
ImageView img=(ImageView)view.findViewById(R.id.img);
TextView txt=(TextView)view.findViewById(R.id.txt);
txt.setText(tab.getTitle());
img.setBackgroundResource(tab.getIcon());
return view;
}
}

这样基本完成


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值