Koltin使用anko可以简化很多findViewById的写法
引入方法,
implementation 'org.jetbrains.anko:anko-commons:0.10.5'//注解,简化findViewById
这样就可以置界使用id了,activity上会多出下面的代码
import kotlinx.android.synthetic.main.activity_main.*
MianActivity.kt
package com.example.takeout.ui.activity
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
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() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initBottomBar()
}
/**
* 初始化底部导航栏的点击事件
*/
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)
}
}
}
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">
</FrameLayout>
<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>
<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>
<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>
<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" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
main_home_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="@mipmap/home_disabled"></item>
<item android:state_enabled="true" android:drawable="@mipmap/home_normal"></item>
</selector>
效果