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">

        <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>


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

line_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_selected="true" android:drawable="@drawable/red_line" />

    <item android:drawable="@drawable/blue_line" />
</selector>


red_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <stroke android:width="1dp" android:color="@android:color/holo_blue_dark" />
</shape>


blue_line.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">

    <stroke android:width="1dp" android:color="@android:color/holo_red_dark" />

</shape>

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"
    android:gravity="center">

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


        <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"/>


        <ImageView
            android:layout_width="16dp"
            android:layout_height="8dp"
            tools:src="@drawable/line_selector"
            android:id="@+id/tab_image" />
    </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(R.drawable.line_selector);
        TextView tabTextView = (TextView) tabView.findViewById(R.id.tab_text);
        tabTextView.setText(tabTitle[index]);
        return tabView;
    }

}


源码下载 http://download.csdn.net/detail/hu285279904/9655659


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值