您的位置:首页 > 其它

FragmentTabHost的使用

2016-03-22 13:51 197 查看
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;

}

}

这样基本完成
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: