很久之前写过一次
效果
但是没有kt版本的 是之前的 甚至都没用androidX
今天再来一次
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="@layout/toolbar" />
<androidx.viewpager.widget.ViewPager
android:id="@+id/amt_vp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/amt_tl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/color_main"
app:tabIndicatorColor="#ffffff"
app:tabSelectedTextColor="@color/white"
app:tabTextColor="@color/gray" />
</LinearLayout>
<include layout="@layout/toolbar" />
toolbar是自己的一个封装
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:titleTextColor="@android:color/white"
app:subtitleTextColor="@android:color/white"
app:theme="@style/toolBar3"
/>
界面
package com.anguomob.camera
class MainActivity2 : BaseActivity(), EasyPermissions.PermissionCallbacks {
lateinit var mAmtTl:TabLayout
lateinit var mAmtVp:ViewPager
// tabLayout标题
lateinit var mListTitle: ArrayList<String>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_tablayout)
initView()
initTabLayout()
initAdapter()
}
private fun initAdapter() {
var find_tab_adapter = HomeTabAdapter(supportFragmentManager, mListTitle)
//viewpager加载adapter
mAmtVp.setAdapter(find_tab_adapter);
//关联指示器
mAmtTl.setupWithViewPager(mAmtVp);
}
private fun initView() {
mAmtTl = findViewById<TabLayout>(R.id.amt_tl);
mAmtVp = findViewById<ViewPager>(R.id.amt_vp);
}
private fun initTabLayout() {
//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
//将名称加载tab名字列表,正常情况下,我们应该在values/arrays.xml中进行定义然后调用
mListTitle = ArrayList<String>()
mListTitle.add(resources.getString(R.string.photo_album))
mListTitle.add(resources.getString(R.string.camera))
mListTitle.add(resources.getString(R.string.pictrue_stitching))
//设置tabLayout的模式
// tlTitle.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加名称
//设置tabLayout的模式
// tlTitle.setTabMode(TabLayout.MODE_FIXED);
//为TabLayout添加名称
for (i in 0..mListTitle.size - 1) {
mAmtTl.addTab(mAmtTl.newTab().setText(mListTitle.get(i)))
}
}
adapter
package com.anguomob.camera.adapter
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import com.anguomob.camera.fragment.PhotoAlbumFragment
import com.anguomob.camera.fragment.PictrueStitchingFragment
/**
* Name: Find_tab_Adapter
* Author: liuan
* creatTime:2017-01-08 07:13
*/
class HomeTabAdapter(fm: FragmentManager, //tab名的列表
private val list_title: List<String>) : FragmentPagerAdapter(fm) {
override fun getItem(position: Int): Fragment {
when (position) {
0 ->return PhotoAlbumFragment()
2 ->return PictrueStitchingFragment()
else->return PhotoAlbumFragment()
}
}
override fun getCount(): Int {
return list_title.size
}
//此方法 用来显示tab上的名字
override fun getPageTitle(position: Int): CharSequence {
return list_title[position % list_title.size]
}
}
PictrueStitchingFragment
package com.anguomob.camera.fragment
import com.anguomob.camera.base.BaseFragment
/**
* Create by: liuan
* Create date: 2020-11-02 0002
* Describe:
*/
class PictrueStitchingFragment: BaseFragment(){
}
PhotoAlbumFragment
package com.anguomob.camera.fragment
import com.anguomob.camera.base.BaseFragment
/**
* Create by: liuan
* Create date: 2020-11-02 0002
* Describe:
*/
class PhotoAlbumFragment : BaseFragment() {
}
就不再赘述了
package com.anguomob.camera.base
import android.graphics.Color
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
/**
* Create by: liuan
* Create date: 2020-11-02 0002
* Describe:
*/
open class BaseFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val textView = TextView(context)
textView.text = javaClass.simpleName
textView.setTextColor(Color.RED)
textView.gravity = Gravity.CENTER
return textView
}
}