上效果图:
MiddleFragment.java 代码如下
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;、
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.gxuwz.yixin.R;
import java.util.List;
import java.util.Map;
// 初中 Fragment
public class MiddleFragment extends Fragment {
private View view;
TabHost tabHost;
String subjectArray[]; // 科目
private List<Map<String,Object>> dataList;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,
@Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_middle,container,false);
initView();
tabHost.setup();
//动态载入xml
inflater.inflate(R.layout.fragment_first_grade, tabHost.getTabContentView());
inflater.inflate(R.layout.fragment_two_grade, tabHost.getTabContentView());
initData();
initEvent();
initAdapter();
return view;
}
public void initView() {
subjectArray = getContext().getResources().getStringArray(R.array.subjects);
tabHost = view.findViewById(android.R.id.tabhost);
}
public void initData() {
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator(subjectArray[0]).setContent(R.id.tab01));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator(subjectArray[1]).setContent(R.id.tab02));
//设置 TabWidget 的布局参数
final TabWidget tabWidget = tabHost.getTabWidget();
// tabHost 选项卡改变的时候
tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
for(int i = 0; i < tabWidget.getChildCount(); i++) {
View tabView = tabWidget.getChildAt(i);
TextView tv= tabWidget.getChildAt(i).findViewById(android.R.id.title);
if(tabHost.getCurrentTab() == i) { // 选中
tabView.setBackgroundResource(R.drawable.tabhost_selected_true);
tv.setTextSize(14);
} else { // 未选中
tabView.setBackgroundResource(R.drawable.tabhost_selected_false);
tv.setTextSize(12);
}
}
}
});
for (int i = 0; i < tabWidget.getChildCount(); i++) {
View view = tabWidget.getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
// 设置默认的选项卡背景颜色与字体大小
TextView textView = tabWidget.getChildAt(i).findViewById(android.R.id.title);
//view.setBackgroundColor(R.color.grey2);
textView.setTextSize(12);
// 设置第一个选项卡被选中时的样式
TextView tvDefault = tabWidget.getChildAt(0).findViewById(android.R.id.title);
tabWidget.getChildAt(0).setBackgroundResource(R.drawable.tabhost_selected_true);
tvDefault.setTextSize(14);
//获取tabs图片;
//ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
layoutParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
layoutParams.height = 130;
layoutParams.weight = 0.0f;
}
}
public void initEvent() {
}
public void initAdapter() {
}
}
fragment_middle 代码如下:
<?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=".activity.MainActivity">
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="90dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="top">
</TabWidget>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
res/values/string.xml 中加入
<string-array name="subjects">
<item>语文</item>
<item>数学</item>
</string-array>
选中的背景样式
tabhost_selected_true.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 连框颜色值 -->
<item android:height="110dp" android:gravity="center_vertical">
<shape>
<solid android:color="@color/blue" />
</shape>
</item>
<!-- 主体背景颜色值 -->
<item android:left="10px" >
<!--边框里面背景颜色 白色-->
<shape>
<solid android:color="@color/white" />
</shape>
</item>
</layer-list>
未选中的背景样式
tabhost_selected_false.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="@color/grey2"></solid>
</shape>
如果缺少哪一个文件,请在评论区留言