Kolin 实现侧抽屉(drawer)菜单
Posted 彬sir哥
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kolin 实现侧抽屉(drawer)菜单相关的知识,希望对你有一定的参考价值。
一、测试
实现:
二、准备工作
1.抽屉式自定义DrawerView.java
class DrawerView : LinearLayout, View.OnClickListener
private var mContext: Context? = null
constructor(context: Context, attributeSet: AttributeSet) : super(context, attributeSet)
LayoutInflater.from(context).inflate(R.layout.view_drawer, this)
mContext = context
initView()
initEvent()
private fun initView()
private fun initEvent()
testtvUserName.setOnClickListener(this)
tavModelName.setOnClickListener(this)
tavTaskMgr.setOnClickListener(this)
override fun onClick(v: View)
when (v.id)
R.id.testtvUserName -> showToast("用户名10023")
R.id.tavModelName -> showToast("用户名10024")
R.id.tavTaskMgr -> showToast("用户名10025")
private fun showToast(msg: String)
Toast.makeText(mContext, msg, Toast.LENGTH_SHORT).show()
view_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/aiv1"
android:layout_width="110dp"
android:layout_height="match_parent"
android:background="#4CAF50"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/testtvUserName"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:drawablePadding="21dp"
android:gravity="center"
android:text="用户名10023"
android:textColor="#000000"
android:textSize="10dp"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="@+id/aview1"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="25dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/testtvUserName" />
<TextView
android:id="@+id/tavModelName"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:drawablePadding="21dp"
android:gravity="center"
android:text="用户名10024"
android:textColor="#000000"
android:textSize="10dp"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/aview1" />
<View
android:id="@+id/aview2"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="25dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/tavModelName" />
<TextView
android:id="@+id/tavTaskMgr"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:drawablePadding="21dp"
android:gravity="center"
android:text="用户名10025"
android:textColor="#000000"
android:textSize="10dp"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/aview2" />
<View
android:id="@+id/aview3"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="25dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/tavTaskMgr" />
<TextView
android:id="@+id/atvOfflineMap"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_marginTop="30dp"
android:drawablePadding="21dp"
android:gravity="center"
android:text="用户名10026"
android:textColor="#000000"
android:textSize="10dp"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/aview3" />
<View
android:id="@+id/aview4"
android:layout_width="0dp"
android:layout_height="1dp"
android:layout_marginTop="25dp"
android:background="@color/colorPrimary"
app:layout_constraintLeft_toLeftOf="@+id/aiv1"
app:layout_constraintRight_toRightOf="@+id/aiv1"
app:layout_constraintTop_toBottomOf="@+id/atvOfflineMap" />
</androidx.constraintlayout.widget.ConstraintLayout>
2.使用工作
主活动
MainActivity.java
class MainActivity : FragmentActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_main.setOnClickListener myDrawer.openDrawer(GravityCompat.START)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/rlMapOperaionView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="左边侧滑,可以出现侧滑菜单,点击用户名10023可以出现二级菜单,再点击可隐藏" />
<Button
android:id="@+id/btn_main"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="Button" />
</LinearLayout>
</FrameLayout>
<com.ufi.pdioms.ztkotlin.DrawerView
android:id="@+id/drawerView_comple"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
</androidx.drawerlayout.widget.DrawerLayout>
以上是关于Kolin 实现侧抽屉(drawer)菜单的主要内容,如果未能解决你的问题,请参考以下文章