Android how can i replace the deprecated tabhost?

青春壹個敷衍的年華 提交于 2021-02-15 06:52:32

问题


I'm going to create an application which is designed to use Tabhost, but as I know it's been deprecated. So my question is should I use Tabhost anyway or how can I replace it..by using buttons instead of them declared in every xml or any other suggestions?


回答1:


"Deprecated" in Android means "we think there is a better solution that you should investigate". Rarely does "deprecated" mean "it is unusable". TabHost, AFAIK, works fine on Android 4.0.

That being said, I would recommend considering switching to tabs in the action bar, using ActionBarSherlock to give you backwards compatibility to Android 2.1.

UPDATE

Besides, TabHost is not deprecated. TabActivity is deprecated. You can still use TabHost, with views for your tabs. Or, use:

  • ViewPager with a tabbed indicator, like PagerTabStrip
  • FragmentTabHost, for a TabHost that uses fragments for tabs

The action bar also has tab support, but that was deprecated starting with the "L" Developer Preview.




回答2:


As per Android API level-18, ActionBarSherlock is not recommended instead of that,they have introduced ActionBarCompat .. Better to go with ActionBarCompat.

Thank you.




回答3:


TabHost was deprecated in API level 30. You can use material TabLayout and ViewPager instead of TabHost:

<com.google.android.material.tabs.TabLayout
            android:id="@+id/tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:tabGravity="center"
            app:tabIndicatorColor="@color/colorPrimary"
            app:tabIndicatorHeight="2dp"
            app:tabMode="fixed"
            app:tabSelectedTextColor="@color/colorPrimary"
            app:tabTextAppearance="@style/MapTabTextStyle"
            app:tabTextColor="@color/black" />

code:

tablayout.addTab(tablayout.newTab().setText("Tab1"))
tablayout.addTab(tablayout.newTab().setText("Tab2"))
tablayout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {

        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
            
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            // do something
        }

    })


来源:https://stackoverflow.com/questions/65915983/tabhost-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!