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;
}
}
这样基本完成
<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;
}
}
这样基本完成
相关文章推荐
- jquery append 动态添加的元素事件on 不起作用的解决方案
- jquery中使用ajax发送post请求返回数据在error中
- Unity3D中JavaScript与C#对比
- [Java]double初始化问题
- 文本 去除重复行(sublime Text3 ,正则表达式)
- HDU-4041-Eliminate Witches! (11年北京网络赛!!)
- PHP抓取网络数据的6种常见方法
- 百度地图3.7.1的卫星地图,实时交通的改变
- ump_player在线直播播放器
- 查找行迁移及消除行迁移(chained rows)
- 正在进行调试的Web服务器进程已由IIS终止
- spring mvc(注解)上传文件的简单例子
- 数据结构(6): 链队——队列的链式表示和实现
- c标签的处理字符串的两个方法
- 后台管理系统模板ACE
- iOS给客户打包ipa 文件
- junit 测试保存 未加@Modifying,@Transactional(readOnly=false)报错
- Activity中UI框架基本概念
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
- #!/bin/bash