3

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

4

2 回答 2

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)
于 2018-02-15T05:50:53.997 回答
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)
于 2017-09-12T19:37:57.717 回答