如何在anko中创建选项卡式视图

时间:2017-09-12 18:25:44

标签: android kotlin anko

我想要创建时间表应用程序,但我在创建选项卡式视图时遇到问题,如图中所示。我试图使用tabhost和tabwidget,但没有效果。是否有可能使用anko构建tabview? Picture

2 个答案:

答案 0 :(得分:1)

如果您的问题是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)

答案 1 :(得分:0)

您必须使用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)