【问题标题】:How create tabbed view in anko如何在 anko 中创建选项卡式视图
【发布时间】:2017-09-12 18:25:44
【问题描述】:

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

【问题讨论】:

    标签: android kotlin anko


    【解决方案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)
    

    【讨论】:

    • 除了你的第一个建议你还应该提供你的版本对象,否则它自己是无法理解的
    • 我在找同样的东西,试过了,但coordinatorLayout无法解决。
    【解决方案2】:

    您必须通过使用 TabLayout 和 ViewPager 使用片段来创建它。 这里是 Kotlin 代码 sn-p

     // 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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多