Android 如何在anko中创建选项卡式视图
我想要创建时间表应用程序,但我在创建选项卡式视图时遇到问题,如图所示。我尝试使用tabhost和tabwidget,但没有效果。是否可以使用anko构建tabview?Android 如何在anko中创建选项卡式视图,android,kotlin,anko,Android,Kotlin,Anko,我想要创建时间表应用程序,但我在创建选项卡式视图时遇到问题,如图所示。我尝试使用tabhost和tabwidget,但没有效果。是否可以使用anko构建tabview? 您必须通过使用片段将TabLayout与ViewPager结合使用来创建它。 下面是Kotlin代码片段 // Create the adapter that will return a fragment for each of the three // primary sections of the activity
您必须通过使用片段将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)
//创建适配器,该适配器将为这三个适配器中的每一个返回一个片段
//活动的主要部分。
mPagerAdapter=PageAdapter(supportFragmentManager,此)
//使用分区适配器设置ViewPager。
mViewPager=findViewById(R.id.container)
MViewPage!!。适配器=兆帕适配器
val tabLayout=findviewbyd(R.id.tabs)作为tabLayout
TableLayout.setupWithViewPager(mViewPager)
//设置图标
tabLayout.getTabAt(0)!!。设置图标(R.drawable.ic_调用)
tabLayout.getTabAt(1)!!。设置图标(R.drawable.ic_fav)
表格布局。getTabAt(2)!!。设置图标(右可牵引ic_触点)
如果您的问题是安科方面,首先您应该使用
“org.jetbrains.anko:anko-support-v4:${versions.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)
除了您的第一个建议之外,您还应该提供您的版本对象,否则其本身无法理解。我正在寻找相同的内容,尝试了此操作,但无法解决
coordinatorLayout
。