布局如下
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