底部导航栏FragmentTabHost
2016-01-22 16:43
267 查看
底部导航栏的实现有多种方式,其中一种就是使用FragmentTabHost来实现。
activity_main.xml
<span style="font-size:18px;"><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"> <FrameLayout android:id="@+id/content" 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" android:background="@color/white"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="0"/> </android.support.v4.app.FragmentTabHost> </LinearLayout> </span>
MainActivity.xml
<span style="font-size:18px;">package com.news.activity;import android.content.Context;import android.content.res.Resources;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;import com.news.fragment.FindFragment;import com.news.fragment.MineFragment;import com.news.fragment.NewsFragment;import com.news.fragment.ReadFragment;import com.news.fragment.VideoFragment;public class MainActivity extends FragmentActivity{private Context mComtext;private FragmentTabHost mTabHost;private LayoutInflater layoutInflater;private Class fragmentArray[];private int mImageViewArray[];private String mTextViewArray[];@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);mComtext=this;initData();initViews();}private void initData(){Resources mResources=mComtext.getResources();fragmentArray = new Class[]{ReadFragment.class,VideoFragment.class,FindFragment.class,MineFragment.class};mImageViewArray=new int[]{R.drawable.tab_read,R.drawable.tab_video,R.drawable.tab_find,R.drawable.tab_mine};mTextViewArray=new String[] {mResources.getString(R.string.read),mResources.getString(R.string.video),mResources.getString(R.string.find),mResources.getString(R.string.mine)};}private void initViews() {layoutInflater=LayoutInflater.from(mComtext);mTabHost=(FragmentTabHost)findViewById(android.R.id.tabhost);mTabHost.setup(mComtext,getSupportFragmentManager(),R.id.content);int count=fragmentArray.length;for(int i=0;i<count;i++){TabHost.TabSpec tabSpec=mTabHost.newTabSpec(mTextViewArray[i]).setIndicator(getTabItemView(i));mTabHost.addTab(tabSpec, fragmentArray[i], null);//去掉分割线mTabHost.getTabWidget().setDividerDrawable(null);}}private View getTabItemView(int index){View view=layoutInflater.inflate(R.layout.activity_tab_item_view,null);ImageView imageView=(ImageView)view.findViewById(R.id.tab_imagev);imageView.setImageResource(mImageViewArray[index]);TextView textView=(TextView)view.findViewById(R.id.tab_textv);textView.setText(mTextViewArray[index]);return view;}}</span>
activity_tab_item_view.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:gravity="center"android:background="@color/white"android:orientation="vertical"><ImageViewandroid:id="@+id/tab_imagev"android:paddingTop="5dp"android:layout_width="wrap_content"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/tab_textv"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textColor="@drawable/tab_item_color"/></LinearLayout></span>tab_item_color.xml
<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="@color/red" android:state_selected="true"/><item android:color="@color/gray"/></selector></span>tab_read.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:drawable="@mipmap/read_h" android:state_selected="true"/><item android:drawable="@mipmap/read"/></selector>
相关文章推荐