{"id":631,"date":"2020-07-07T11:37:04","date_gmt":"2020-07-07T03:37:04","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=631"},"modified":"2020-07-14T13:10:47","modified_gmt":"2020-07-14T05:10:47","slug":"material-design-tabs","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/material-design-tabs\/","title":{"rendered":"Material Design Tabs"},"content":{"rendered":"<h3>1.\u5148\u5c0e\u5165\u51fd\u793a\u5eab(build.gradle: Module)<\/h3>\n<pre><code class=\"language-Gradle line-numbers\">dependencies {\n   implementation 'com.google.android.material:material:1.3.0-alpha01'\n}\n<\/code><\/pre>\n<h3>2.\u7a0b\u5f0f\u78bc\u7bc4\u4f8b<\/h3>\n<h4>a.Layout\u5e03\u5c40<\/h4>\n<pre><code class=\"language-XML line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;androidx.constraintlayout.widget.ConstraintLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    android:id=\"@+id\/blobLayout\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    tools:context=\".MainActivity\"&gt;\n\n    &lt;com.google.android.material.tabs.TabLayout\n        android:background=\"#ffffaa\"\n        android:id=\"@+id\/tabLayout\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"wrap_content\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n    &lt;androidx.viewpager.widget.ViewPager\n        android:id=\"@+id\/viewPage\"\n        android:layout_width=\"0dp\"\n        android:layout_height=\"0dp\"\n        app:layout_constraintBottom_toBottomOf=\"parent\"\n        app:layout_constraintEnd_toEndOf=\"parent\"\n        app:layout_constraintStart_toStartOf=\"parent\"\n        app:layout_constraintTop_toBottomOf=\"@+id\/tabLayout\" \/&gt;\n\n&lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n<\/code><\/pre>\n<h4>b.TabLayout\u8a2d\u7f6e<\/h4>\n<pre><code class=\"language-Kotlin line-numbers\">val mTitles = listOf(\"\u982d\u689d\",\"\u5546\u696d\",\"\u5a1b\u6a02\",\"\u5065\u5eb7\",\"\u79d1\u5b78\", \"\u9ad4\u80b2\")\n\n\/\/\u8a2d\u7f6e\u6efe\u52d5\ntabLayout.tabMode = TabLayout.MODE_SCROLLABLE\n\/\/\u8a2d\u7f6e\u9078\u8207\u672a\u9078\u984f\u8272\ntabLayout.setTabTextColors(Color.BLACK, Color.RED)\n\/\/\u8a2d\u7f6e\u4e0b\u6ed1\u7dda\u984f\u8272\ntabLayout.setSelectedTabIndicatorColor(Color.BLUE)\n\/\/\u8a2d\u7f6e\u6587\u5b57\nmTitles.forEach {\n    tabLayout.addTab(tabLayout.newTab().setText(it))\n}\n\/\/\u8a2d\u7f6e\u53d6\u6d88\u4e0b\u6ed1\u7dda\n\/\/tabLayout.setSelectedTabIndicator(null)\n\/\/\u8a2d\u7f6e\u4e0b\u6ed1\u7dda\u5bec\u5ea6\n\/\/ tabLayout.isTabIndicatorFullWidth = false\n\/\/\u8a2d\u7f6e\u4e0b\u6ed1\u7dda\u9ad8\u5ea6\n\/\/tabLayout.setSelectedTabIndicatorHeight(30)\n\n\/\/\u5ba2\u88fd\u5316View\nfor (i in mTitles.indices) {\n     val view = LayoutInflater.from(this).inflate(R.layout.icon_item, null)\n     val title = view.findViewById&lt;TextView&gt;(R.id.title)\n     title.text = mTitles[i]\n     tabLayout.getTabAt(i)?.customView = view\n}\n<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/07\/tabLayout.jpg\" alt=\"\" \/><\/p>\n<h4>c.Tabs &amp; ViewPage &amp; Fragment<\/h4>\n<h5>FragmentPagerAdapter\u7528\u4f86\u653e\u5165fragment<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">class FragmentAdapter(fm : FragmentManager, behavior: Int, private val mTitle: List&lt;String&gt;, private val fragments : List&lt;Fragment&gt;) : FragmentPagerAdapter(fm, behavior) {\n\n    override fun getItem(position: Int) = fragments[position]\n\n    override fun getCount() = fragments.size\n\n    override fun getPageTitle(position: Int) = mTitle[position]\n\n}\n<\/code><\/pre>\n<h5>Tabs\u8207ViewPager\u7d81\u5b9a<\/h5>\n<pre><code class=\"language-Kotlin line-numbers\">val fragments = mutableListOf&lt;Fragment&gt;()\nfragments.add(Fragment1())\nfragments.add(Fragment2())\nfragments.add(Fragment3())\nfragments.add(Fragment4())\nfragments.add(Fragment5())\nfragments.add(Fragment6())\n\n\/\/\u7d81\u5b9a\ntabLayout.setupWithViewPager(viewPage, false)\n\nviewPage.adapter = FragmentAdapter(supportFragmentManager, tabLayout.tabCount, mTitles,fragments)\nviewPage.currentItem = 0\n<\/code><\/pre>\n<h3>4.\u6548\u679c\u5c55\u793a<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/badgameshow.com\/fly\/wp-content\/uploads\/2020\/07\/tabs.gif\" alt=\"\" \/><\/p>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>1.\u5148\u5c0e\u5165\u51fd\u793a\u5eab(build.gradle: Module) dependencies { implement &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[76],"tags":[13,78,109],"class_list":["post-631","post","type-post","status-publish","format-standard","hentry","category-material-design","tag-android","tag-material-design","tag-tabs"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/631","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=631"}],"version-history":[{"count":3,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/631\/revisions"}],"predecessor-version":[{"id":637,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/631\/revisions\/637"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}