android fragmentTabHost的使用


布局如下

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context="net.sytm.fragmenttabhost.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <FrameLayout
            android:id="@+id/real_tab_content"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <android.support.v4.app.FragmentTabHost
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tab_host">

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="0" />

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

    </LinearLayout>
</RelativeLayout>


tab_check_in_btn.xml

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!--不可以使用 android:state_pressed属性,
    并且前景色放在前面,否则都selector 不好使 -->

    <item android:drawable="@drawable/check_in_sel"
        android:state_selected="true" />
    <item android:drawable="@drawable/check_in_nor" />
</selector>


tab_view.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_margin="8dp"
        android:orientation="vertical">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:src="@drawable/tab_check_in_btn"
            android:id="@+id/tab_image" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="考勤"
            android:id="@+id/tab_text"
            android:textColor="@color/colorTabTextBg"
            android:textSize="10sp"/>
    </LinearLayout>

</LinearLayout>



MainActivity.java


package net.sytm.fragmenttabhost;

import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private int[] tabImage;
    private String[] tabTitle;
    private FragmentTabHost tabHost;
    private Class[] fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //图标
        tabImage = new int[]{R.drawable.tab_check_in_btn, R.drawable.tab_calender_btn,
                R.drawable.tab_msg_btn, R.drawable.tab_tools_btn};
        //标题
        tabTitle = new String[]{"考勤", "日程", "消息", "工具"};

        fragment = new Class[]{CheckInFragment.class, CalenderFragment.class, MessageFragment.class, ToolsFragment.class, };
        tabHost = (FragmentTabHost) findViewById(R.id.tab_host);
        tabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content);
        for (int i = 0; i < tabTitle.length; i++) {
            FragmentTabHost.TabSpec tab = tabHost.newTabSpec(tabTitle[i]).setIndicator(getTabView(i));
            tabHost.addTab(tab, fragment[i], null);

        }
    }

    /**
     * 获取tabview
     * @param index
     * @return
     */
    private View getTabView(int index) {
        View tabView = LayoutInflater.from(this).inflate(R.layout.tab_view, null);
        ImageView tabImageView = (ImageView) tabView.findViewById(R.id.tab_image);
        tabImageView.setImageResource(tabImage[index]);
        TextView tabTextView = (TextView) tabView.findViewById(R.id.tab_text);
        tabTextView.setText(tabTitle[index]);
        return tabView;
    }

}

效果图



源码下载地址 http://download.csdn.net/detail/hu285279904/9655603
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值