日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)

發(fā)布時(shí)間:2025/3/21 Android 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

上效果圖:

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@Overridepublic View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {view = inflater.inflate(R.layout.fragment_middle,container,false);initView();tabHost.setup();//動(dòng)態(tài)載入xmlinflater.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));//設(shè)置 TabWidget 的布局參數(shù)final TabWidget tabWidget = tabHost.getTabWidget();// tabHost 選項(xiàng)卡改變的時(shí)候tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {@Overridepublic 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();// 設(shè)置默認(rèn)的選項(xiàng)卡背景顏色與字體大小TextView textView = tabWidget.getChildAt(i).findViewById(android.R.id.title);//view.setBackgroundColor(R.color.grey2);textView.setTextSize(12);// 設(shè)置第一個(gè)選項(xiàng)卡被選中時(shí)的樣式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"><TabHostandroid:id="@android:id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_alignParentLeft="true"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="90dp"android:layout_height="match_parent"android:orientation="vertical"android:gravity="top"></TabWidget><FrameLayoutandroid: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>語(yǔ)文</item><item>數(shù)學(xué)</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>

如果缺少哪一個(gè)文件,請(qǐng)?jiān)谠u(píng)論區(qū)留言

《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀

總結(jié)

以上是生活随笔為你收集整理的Android --- TabHost 切换时,改变选项卡下字体的状态(大小、加粗、默认被选中第一个)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。