android 底部菜单写法之FragmentTabHost

来源:互联网 发布:java的作用 编辑:程序博客网 时间:2024/05/29 04:20

android底部菜单写法很多种类,今天用的是FragmentTabHost:代码很简单:

Mainacitivity中代码:BaseActivity继承的FragmentActivity

@ContentView(R.layout.activity_main)public class MainActivity extends BaseActivity {    private String[] names = {"书架", "书城", "我的"};    private String[] tags = {"bookshelf", "bookshop", "aboutme"};    private Class[] fragmentClass = {BookshelfFragment.class, BookshopFragment.class, AboutmeFragment.class};    private int[] images = {R.drawable.tab_bookshelf_selector, R.drawable.tab_bookshop_selector, R.drawable.tab_aboutme_selector,};    @ViewInject(android.R.id.tabhost)    private FragmentTabHost tabHost;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        x.view().inject(this);        tabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);        tabHost.getTabWidget().setDividerDrawable(null);        for (int i = 0; i < images.length; i++) {            TabHost.TabSpec tabSpec = tabHost.newTabSpec(tags[i]).setIndicator(getImageView(i));            tabHost.addTab(tabSpec, fragmentClass[i], null);            tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.color.material_grey_100);        }    }    private View getImageView(int index) {        View view = View.inflate(this, R.layout.item_tab, null);        ImageView iv = (ImageView) view.findViewById(R.id.iv_icon);        TextView tv = (TextView) view.findViewById(R.id.tv_name);        iv.setImageResource(images[index]);        tv.setText(names[index]);        return view;    }}


布局文件activity_main代码,注意其中的几个id

<?xml version="1.0" encoding="utf-8"?><android.support.v4.app.FragmentTabHost    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost"    android:layout_width="match_parent"    android:layout_height="match_parent"    >    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent">        <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="match_parent"            android:layout_height="match_parent"/>        <TabWidget            android:id="@android:id/tabs"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_alignParentBottom="true"/>    </RelativeLayout></android.support.v4.app.FragmentTabHost>

布局文件item_tab代码:

<?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:orientation="vertical"              android:padding="3dp">    <ImageView        android:id="@+id/iv_icon"        android:layout_width="30dp"        android:layout_height="30dp"        />    <TextView        android:id="@+id/tv_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:textColor="@drawable/tab_textcolor_selector"/></LinearLayout>

选择器tab_textcolor_selector代码:

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:color="@color/orange" android:state_selected="true"/>    <item android:color="@color/black"/></selector>

最终效果:






阅读全文
0 0
原创粉丝点击