欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義view實現(xiàn)標簽欄功能(只支持固定兩個標簽)

 更新時間:2020年06月07日 11:08:31   作者:安卓007  
這篇文章主要介紹了Android自定義view實現(xiàn)標簽欄(只支持固定兩個標簽),本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

實現(xiàn)效果圖

主要代碼

完整源代碼

class TabView(context: Context, attributeSet: AttributeSet?) : LinearLayout(context, attributeSet) {
 private lateinit var firstTab: View
 private lateinit var secondTab: View
 private val firstTabIndex = 0
 private val secondTabIndex = 1
 private var selectedTab = firstTabIndex
 private val textSize = 20f
 private val bottomSplitColor = "#FA871E"
 private val centerSplitColor = "#666666"
 private val bottomSplitWidth = 50
 private val bottomSplitHeight = 4
 private val centerSplitWidth = 1
 private val centerSplitHeight = 40
 private lateinit var mOnSwitchListener: OnSwitchListener
 fun initTabs(
  firstTabText: String,
  secondTabText: String,
  selectedIndex: Int,
  onSwitchListener: OnSwitchListener
 ) {
  mOnSwitchListener = onSwitchListener
  setOrientation()
  firstTab = addTab(firstTabText)
  addCenterSplit()
  secondTab = addTab(secondTabText)
  selectTab(selectedIndex)
  setOnClickListener { switchTab() }
 }
 interface OnSwitchListener {
  fun onSwitched(selectedIndex: Int)
 }
 private fun selectTab(tabIndex: Int) {
  if (tabIndex == firstTabIndex) {
   firstTab.visibility = View.VISIBLE
   secondTab.visibility = View.INVISIBLE
  } else {
   firstTab.visibility = View.INVISIBLE
   secondTab.visibility = View.VISIBLE
  }
  selectedTab = tabIndex
 }
 private fun switchTab() {
  if (selectedTab == firstTabIndex) {
   selectTab(secondTabIndex)
  } else {
   selectTab(firstTabIndex)
  }
  mOnSwitchListener.onSwitched(selectedTab)
 }
 private fun setOrientation() {
  orientation = HORIZONTAL
 }
 private fun getBottomSplitView(): View {
  val view = View(context)
  view.setBackgroundColor(Color.parseColor(bottomSplitColor))
  return view
 }
 private fun getBottomSplitLayoutParams(): LayoutParams {
  val layoutParams = LayoutParams(bottomSplitWidth, bottomSplitHeight)
  layoutParams.setMargins(3, 3, 3, 3)
  layoutParams.gravity = Gravity.CENTER_HORIZONTAL
  return layoutParams
 }
 private fun addCenterSplit() {
  val view = View(context)
  view.setBackgroundColor(Color.parseColor(centerSplitColor))
  addView(view, getCenterSplitLayoutParams())
 }
 private fun getCenterSplitLayoutParams(): LayoutParams {
  val layoutParams = LayoutParams(centerSplitWidth, centerSplitHeight)
  layoutParams.setMargins(3, 0, 3, 0)
  layoutParams.gravity = Gravity.CENTER_VERTICAL
  return layoutParams
 }
 private fun addTab(text: String): View {
  var linearLayout = LinearLayout(context)
  linearLayout.orientation = VERTICAL
  val textView = getTextView(text)
  linearLayout.addView(
   textView,
   LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
  )
  val splitView = getBottomSplitView()
  linearLayout.addView(splitView, getBottomSplitLayoutParams())
  addView(linearLayout, LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT))
  return splitView
 }
 private fun getTextView(text: String): TextView {
  val textView = TextView(context)
  textView.text = text
  textView.setPadding(10, 10, 10, 10)
  textView.textSize = textSize
  return textView
 }
}

https://gitee.com/cxyzy1/custTabView

總結(jié)

到此這篇關(guān)于Android自定義view實現(xiàn)標簽欄功能(只支持固定兩個標簽)的文章就介紹到這了,更多相關(guān)android自定義view標簽欄內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 分析Android App中內(nèi)置換膚功能的實現(xiàn)方式

    分析Android App中內(nèi)置換膚功能的實現(xiàn)方式

    這篇文章主要介紹了Android App中內(nèi)置換膚功能的實現(xiàn)方式,文中舉了一個類似QQ空間中換膚方式的例子作為說明,需要的朋友可以參考下
    2016-02-02
  • android特賣列表倒計時卡頓問題的解決方法

    android特賣列表倒計時卡頓問題的解決方法

    這篇文章主要為大家詳細介紹了android特賣列表倒計時卡頓問題的解決方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-09-09
  • A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置小結(jié)

    A07_TimePicker & DatePicker & AnalogClock & Digi

    本文將帶領(lǐng)大家一起學習時間日期和時鐘的設(shè)置。A07_TimePicker & DatePicker & AnalogClock & DigitalClock 的設(shè)置,感興趣的朋友可以參考下哈
    2013-06-06
  • android獲取相冊圖片和路徑的實現(xiàn)方法

    android獲取相冊圖片和路徑的實現(xiàn)方法

    這篇文章主要介紹了android獲取相冊圖片和路徑的實現(xiàn)方法,本文介紹的是Android4.4后的方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • Android中用Builder模式自定義Dialog的方法

    Android中用Builder模式自定義Dialog的方法

    在任何軟件操作系統(tǒng)中,Dialog即對話框都是一種重要的交互模式與信息載體,而Android系統(tǒng)本身的Dialog擁有固定的樣式,并且在5.0后采用Material Design設(shè)計風格的Dialog美觀大氣。這篇文章將詳細介紹Android中用Builder模式自定義Dialog的方法,有需要的可以參考借鑒。
    2016-10-10
  • Android基于OpenCV實現(xiàn)Harris角點檢測

    Android基于OpenCV實現(xiàn)Harris角點檢測

    角點就是極值點,即在某方面屬性特別突出的點。當然,你可以自己定義角點的屬性(設(shè)置特定熵值進行角點檢測)。角點可以是兩條線的交叉處,也可以是位于相鄰的兩個主要方向不同的事物上的點。本文介紹如何基于OpenCV實現(xiàn)Harris角點檢測
    2021-06-06
  • ionic2如何處理android硬件返回按鈕

    ionic2如何處理android硬件返回按鈕

    這篇文章主要為大家詳細介紹了ionic2如何處理android硬件返回按鈕,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • Flutter 完美的驗證碼輸入框?qū)崿F(xiàn)

    Flutter 完美的驗證碼輸入框?qū)崿F(xiàn)

    這篇文章主要介紹了Flutter 完美的驗證碼輸入框?qū)崿F(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-04-04
  • Android實現(xiàn)View拖拽跟隨手指移動效果

    Android實現(xiàn)View拖拽跟隨手指移動效果

    這篇文章主要介紹了Android實現(xiàn)View拖拽跟隨手指移動效果,主要使用setTranslationX() 和setTranslationY() 屬性方法實現(xiàn)的,需要的朋友參考下吧
    2017-08-08
  • 安卓模擬器genymotion的安裝與使用圖文教程

    安卓模擬器genymotion的安裝與使用圖文教程

    這篇文章主要為大家詳細介紹了安卓模擬器genymotion的安裝與使用圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2016-08-08

最新評論