【原创】android中实现底部tabhost
16lz
2021-01-25
这个主要是实现底部的tabhost方式,tabhost就是有几个标签滑动的一个控件。activity继承TabActivity
其他不多说了,直接上代码
这个是主类
再看看主布局文件:
这样,基本上一个可用的tabhost就可以了。
附件是代码
其他不多说了,直接上代码
public class main extends TabActivity {private TabHost tabHost;private TabWidget tabWidget;Field mBottomLeftStrip;Field mBottomRightStrip;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);makeTab();}public void makeTab() {if (this.tabHost == null) {tabHost = getTabHost();tabWidget = getTabWidget();tabHost.setup();tabHost.bringToFront();TabSpec firsttab = tabHost.newTabSpec("firsttab");TabSpec sencondtab = tabHost.newTabSpec("sencondtab");TabSpec thirdtab = tabHost.newTabSpec("thirdtab");firsttab.setIndicator("first",getResources().getDrawable(R.drawable.first)).setContent(new Intent(this, first.class));sencondtab.setIndicator("second",getResources().getDrawable(R.drawable.second)).setContent(new Intent(this, second.class));thirdtab.setIndicator("third",getResources().getDrawable(R.drawable.third)).setContent(new Intent(this, third.class));tabHost.addTab(firsttab);tabHost.addTab(sencondtab);tabHost.addTab(thirdtab);if (Integer.valueOf(Build.VERSION.SDK) <= 7) {try {mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mBottomLeftStrip");mBottomRightStrip = tabWidget.getClass().getDeclaredField("mBottomRightStrip");if (!mBottomLeftStrip.isAccessible()) {mBottomLeftStrip.setAccessible(true);}if (!mBottomRightStrip.isAccessible()) {mBottomRightStrip.setAccessible(true);}mBottomLeftStrip.set(tabWidget,getResources().getDrawable(R.drawable.linee));mBottomRightStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));} catch (Exception e) {e.printStackTrace();}} else {try {mBottomLeftStrip = tabWidget.getClass().getDeclaredField("mLeftStrip");mBottomRightStrip = tabWidget.getClass().getDeclaredField("mRightStrip");if (!mBottomLeftStrip.isAccessible()) {mBottomLeftStrip.setAccessible(true);}if (!mBottomRightStrip.isAccessible()) {mBottomRightStrip.setAccessible(true);}mBottomLeftStrip.set(tabWidget,getResources().getDrawable(R.drawable.linee));mBottomRightStrip.set(tabWidget, getResources().getDrawable(R.drawable.linee));} catch (Exception e) {e.printStackTrace();}}for (int i = 0; i < tabWidget.getChildCount(); i++) {View view = tabWidget.getChildAt(i);if (tabHost.getCurrentTab() == i) {view.setBackgroundDrawable(getResources().getDrawable(R.drawable.focus));} else {view.setBackgroundDrawable(getResources().getDrawable(R.drawable.unfocus));}}tabHost.setOnTabChangedListener(new OnTabChangeListener() {@Overridepublic void onTabChanged(String tabId) {for (int i = 0; i < tabWidget.getChildCount(); i++) {View view = tabWidget.getChildAt(i);Toast.makeText(main.this, tabId, Toast.LENGTH_SHORT).show();if (tabHost.getCurrentTab() == i) {view.setBackgroundDrawable(getResources().getDrawable(R.drawable.focus));} else {view.setBackgroundDrawable(getResources().getDrawable(R.drawable.unfocus));}}}});}}}
这个是主类
再看看主布局文件:
<?xml version="1.0" encoding="utf-8"?><TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1"> </FrameLayout> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_alignParentBottom="true" android:layout_height="wrap_content"/> </LinearLayout></TabHost>
这样,基本上一个可用的tabhost就可以了。
附件是代码
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Andoroid学习笔记05——Activity布局管理
- RatingBar的使用方法
- 开发问题及解决 java.lang.ClassCastException:android.widget.L
- Android(安卓)Brightness Changing
- ScrollView包含其他可scroll的view时保持滑到最顶端
- MediaRecorderClient决定使用stagefright还是opencore实现录音录
- listview中放Button 点击 长按事件
- Android(安卓)glide-transformations 使用demo 实现Blur等效果