如何在 anko 中创建标签视图
How create tabbed view in anko
我想创建时间表应用程序,但我在创建如图所示的选项卡式视图时遇到问题。我尝试使用 tabhost 和 tabwidget,但没有效果。是否可以使用 anko 构建 tabview?
Picture
您必须使用片段将 TabLayout 与 ViewPager 结合使用来创建它。
这是 Kotlin 代码片段
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mPagerAdapter = PageAdapter(supportFragmentManager, this)
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById<ViewPager?>(R.id.container)
mViewPager!!.adapter = mPagerAdapter
val tabLayout = findViewById<View>(R.id.tabs) as TabLayout
tabLayout.setupWithViewPager(mViewPager)
// set icons
tabLayout.getTabAt(0)!!.setIcon(R.drawable.ic_call)
tabLayout.getTabAt(1)!!.setIcon(R.drawable.ic_fav)
tabLayout.getTabAt(2)!!.setIcon(R.drawable.ic_contacts)
如果你的问题是anko方面,首先你应该使用
"org.jetbrains.anko:anko-support-v4:${versions.anko}"
然后anko代码可能是这样的
coordinatorLayout {
lparams(matchParent, matchParent)
appBarLayout {
lparams(matchParent, wrapContent)
myTabLayout = themedTabLayout(R.style.ThemeOverlay_AppCompat_Dark) {
lparams(matchParent, wrapContent)
{
tabGravity = Gravity.FILL
tabMode = TabLayout.MODE_FIXED
}
}
}
myViewPager = viewPager {
id = R.id.viewpager
}.lparams(matchParent, matchParent)
(myViewPager!!.layoutParams as CoordinatorLayout.LayoutParams).behavior = AppBarLayout.ScrollingViewBehavior()
}
最后,kotlin 方面可以像@Saurabh 的解决方案一样:
mPagerAdapter = PageAdapter(supportFragmentManager, this)
// Set up the ViewPager with the sections adapter.
myViewPager!!.adapter = mPagerAdapter
myTtabLayout.setupWithViewPager(myViewPager)
// set icons
myTabLayout.getTabAt(0)!!.setIcon(R.drawable.ic_call)
myTabLayout.getTabAt(1)!!.setIcon(R.drawable.ic_fav)
myTabLayout.getTabAt(2)!!.setIcon(R.drawable.ic_contacts)
我想创建时间表应用程序,但我在创建如图所示的选项卡式视图时遇到问题。我尝试使用 tabhost 和 tabwidget,但没有效果。是否可以使用 anko 构建 tabview? Picture
您必须使用片段将 TabLayout 与 ViewPager 结合使用来创建它。 这是 Kotlin 代码片段
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mPagerAdapter = PageAdapter(supportFragmentManager, this)
// Set up the ViewPager with the sections adapter.
mViewPager = findViewById<ViewPager?>(R.id.container)
mViewPager!!.adapter = mPagerAdapter
val tabLayout = findViewById<View>(R.id.tabs) as TabLayout
tabLayout.setupWithViewPager(mViewPager)
// set icons
tabLayout.getTabAt(0)!!.setIcon(R.drawable.ic_call)
tabLayout.getTabAt(1)!!.setIcon(R.drawable.ic_fav)
tabLayout.getTabAt(2)!!.setIcon(R.drawable.ic_contacts)
如果你的问题是anko方面,首先你应该使用
"org.jetbrains.anko:anko-support-v4:${versions.anko}"
然后anko代码可能是这样的
coordinatorLayout {
lparams(matchParent, matchParent)
appBarLayout {
lparams(matchParent, wrapContent)
myTabLayout = themedTabLayout(R.style.ThemeOverlay_AppCompat_Dark) {
lparams(matchParent, wrapContent)
{
tabGravity = Gravity.FILL
tabMode = TabLayout.MODE_FIXED
}
}
}
myViewPager = viewPager {
id = R.id.viewpager
}.lparams(matchParent, matchParent)
(myViewPager!!.layoutParams as CoordinatorLayout.LayoutParams).behavior = AppBarLayout.ScrollingViewBehavior()
}
最后,kotlin 方面可以像@Saurabh 的解决方案一样:
mPagerAdapter = PageAdapter(supportFragmentManager, this)
// Set up the ViewPager with the sections adapter.
myViewPager!!.adapter = mPagerAdapter
myTtabLayout.setupWithViewPager(myViewPager)
// set icons
myTabLayout.getTabAt(0)!!.setIcon(R.drawable.ic_call)
myTabLayout.getTabAt(1)!!.setIcon(R.drawable.ic_fav)
myTabLayout.getTabAt(2)!!.setIcon(R.drawable.ic_contacts)