Koltin16,androidstudio蓝牙应用开发

import androidx.appcompat.app.AppCompatActivity

import com.example.takeout.R

import com.example.takeout.ui.fragment.HomeFragment

import com.example.takeout.ui.fragment.MoreFragment

import com.example.takeout.ui.fragment.OrderFragment

import com.example.takeout.ui.fragment.UserFragment

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

val fragments: List = listOf(HomeFragment(), OrderFragment(), UserFragment(), MoreFragment())

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContentView(R.layout.activity_main)

initBottomBar()

changeIndex(0)//默认选中第0个

}

/**

  • 初始化底部导航栏的点击事件

*/

private fun initBottomBar() {

for (i in 0 until main_bottom_bar.childCount) {

main_bottom_bar.getChildAt(i).setOnClickListener {

view ->

changeIndex(i)

}

}

}

/**

  • 改变index

*/

private fun changeIndex(index: Int) {

for (i in 0 until main_bottom_bar.childCount) {

val child = main_bottom_bar.getChildAt(i)

if (i == index) {

//选中,禁用效果

setEnable(child, false)

} else {

//没选中的,enable=true

setEnable(child, true)

}

}

fragmentManager.beginTransaction().replace(R.id.main_content, fragments[index]).commit()

}

private fun setEnable(child: View, isEnable: Boolean) {

//koltin设置enable的方法

child.isEnabled = isEnable

//递归

if (child is ViewGroup) {

for (i in 0 until child.childCount) {

child.getChildAt(i).isEnabled = isEnable

}

}

}

}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<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/ll_main_activity"

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:orientation=“vertical”>

<FrameLayout

android:id="@+id/main_content"

android:layout_width=“match_parent”

android:layout_height=“0dp”

android:layout_weight=“1”>

<LinearLayout

android:id="@+id/main_bottom_bar"

android:layout_width=“match_parent”

android:layout_height=“50dp”

android:layout_weight=“0”

android:orientation=“horizontal”>

<FrameLayout

android:layout_width=“0dp”

android:layout_height=“match_parent”

android:layout_weight=“1”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“30dp”

android:src="@drawable/main_home_selector" />

<TextView

android:layout_width=“match_parent”

android:layout_height=“20dp”

android:layout_gravity=“bottom”

android:gravity=“center”

android:text=“首页”

android:textColor="@color/main_bottom_text_color" />

<FrameLayout

android:layout_width=“0dp”

android:layout_height=“match_parent”

android:layout_weight=“1”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“30dp”

android:src="@drawable/main_order_selector" />

<TextView

android:layout_width=“match_parent”

android:layout_height=“20dp”

android:layout_gravity=“bottom”

android:gravity=“center”

android:text=“订单”

android:textColor="@color/main_bottom_text_color" />

<FrameLayout

android:layout_width=“0dp”

android:layout_height=“match_parent”

android:layout_weight=“1”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“30dp”

android:src="@drawable/main_user_selector" />

<TextView

android:layout_width=“match_parent”

android:layout_height=“20dp”

android:layout_gravity=“bottom”

android:gravity=“center”

android:text=“个人”

android:textColor="@color/main_bottom_text_color" />

<FrameLayout

android:layout_width=“0dp”

android:layout_height=“match_parent”

android:layout_weight=“1”>

<ImageView

android:layout_width=“match_parent”

android:layout_height=“30dp”

android:src="@drawable/main_more_selector" />

<TextView

android:layout_width=“match_parent”

android:layout_height=“20dp”

android:layout_gravity=“bottom”

android:gravity=“center”

android:text=“更多”

android:textColor="@color/main_bottom_text_color" />

HomeFragment.kt

package com.example.takeout.ui.fragment

import android.app.Fragment

import

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

android.os.Bundle

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import com.example.takeout.R

class HomeFragment : Fragment() {

override fun onCreateView(

inflater: LayoutInflater,

container: ViewGroup?,

savedInstanceState: Bundle?

): View? {

return View.inflate(activity, R.layout.fragment_, null)

}

}

OrderFragment.kt

package com.example.takeout.ui.fragment

import android.app.Fragment

import android.os.Bundle

import android.view.LayoutInflater

import android.view.View

import android.view.ViewGroup

import android.widget.TextView

上一篇:将一层数据处理成树形结构


下一篇:使用斯坦福工具进行句法分析