效果图如下
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>
<?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