您的位置:首页 > 其它

fragmentTabhost+Fragment底部菜单实现方法

2016-01-10 19:55 513 查看
最近在研究底部菜单的实现方法,之前用的是viewpager+fragment和radiobutton+fragment,但是代码非常多,谷歌在最新的api上用了fragmentTabhost实现起来比较简单,好了废话不多说,先看运行效果。

1.今天的知识点:fragmentTabhost的用法和selector的用法



下面直接上代码:

activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns: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/maincontent"
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:layout_weight="0">

</FrameLayout>

</android.support.v4.app.FragmentTabHost>

</LinearLayout>

tab_indicator.xml

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="3dp"
android:paddingTop="3dp">

<ImageView
android:id="@+id/image_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="@string/app_name"/>

<TextView
android:id="@+id/text_tab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"/>

</LinearLayout>


MainActivity.java


packagebailead.com.sports;

importandroid.os.Bundle;
importandroid.support.v4.app.FragmentActivity;
importandroid.support.v4.app.FragmentTabHost;
importandroid.view.LayoutInflater;
importandroid.view.View;
importandroid.widget.ImageView;
importandroid.widget.LinearLayout;
importandroid.widget.TabHost;
importandroid.widget.TextView;

importjava.util.ArrayList;
importjava.util.List;

importbailead.com.sports.entity.Tab;
importbailead.com.sports.fragment.HomeFragment;
importbailead.com.sports.fragment.IntroFragment;
importbailead.com.sports.fragment.SettingFragment;

/**
*CreatedbyAdministratoron2016/1/9.
*/
publicclassMainActivityextendsFragmentActivity{

privateFragmentTabHostmTabHost;
privateLayoutInflatermInflater;

privateList<Tab>list=newArrayList<>();

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTab();
}

privatevoidinitTab(){
Tabhome=newTab(R.string.string_tab_home,R.drawable.selcet_image_home,HomeFragment.class);
Tabintro=newTab(R.string.string_tab_intro,R.drawable.selcet_image_intro,IntroFragment.class);
Tabsetting=newTab(R.string.string_tab_setting,R.drawable.selcet_image_setting,SettingFragment.class);
list.add(home);
list.add(intro);
list.add(setting);
mInflater=LayoutInflater.from(MainActivity.this);
mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(MainActivity.this,getSupportFragmentManager(),R.id.maincontent);

for(Tabtab:list){
TabHost.TabSpectabSpec=mTabHost.newTabSpec(String.valueOf(tab.getTitle()));
tabSpec.setIndicator(buildIndicator(tab));
mTabHost.addTab(tabSpec,tab.getFragment(),null);
}
//去掉分割线
mTabHost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);
mTabHost.setCurrentTab(0);
}

publicViewbuildIndicator(Tabtab){
Viewview=mInflater.inflate(R.layout.tab_indicator,null);
ImageViewimageView=(ImageView)view.findViewById(R.id.image_tab);
TextViewtextView=(TextView)view.findViewById(R.id.text_tab);
imageView.setBackgroundResource(tab.getImage());
textView.setText(tab.getTitle());
returnview;
}

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