废话不多说,直接上代码。这是结果展示:
首先, 修改values文件夹下的style.xml文件,设置没有actionbar的风格。 <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> <style name="AppTheme.NoActionBar" parent="Theme.AppCompat.Light.NoActionBar"> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> </style> </resources> 之后,在AndroidMainifest.xml中,更改风格为我们设置的AppTheme.NoActionBar。我们在layout文件夹中,创建toolbar的样式,为了提高复用性,我们单独为它设置一个xml文件,代码如下: <?xml version="1.0" encoding="utf-8"?> <androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize"> </androidx.appcompat.widget.Toolbar> 我们创建我们的主要界面。bar_draw.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:toots="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" toots:context=".ToolActivity"> <include layout="@layout/tool_bar"/> <androidx.drawerlayout.widget.DrawerLayout android:id="@+id/draw_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#6587BAC7"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="内容" android:textSize="30sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFEB3B" android:orientation="vertical" android:layout_gravity="start" > <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="滑动页面" android:gravity="center" android:clickable="true" /> </LinearLayout> </androidx.drawerlayout.widget.DrawerLayout> </LinearLayout> 设置主活动代码 import android.app.Activity import android.os.Bundle import android.view.Menu import android.widget.Toast import androidx.appcompat.app.ActionBarDrawerToggle import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.widget.Toolbar import androidx.drawerlayout.widget.DrawerLayout class ToolActivity : AppCompatActivity(){ private var mToolbar:Toolbar? = null private var mDrawerLayout:DrawerLayout? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.bar_draw) initView() } fun initView():Unit{ mToolbar = this.findViewById(R.id.toolbar) mToolbar!!.title = "Toolbar" setSupportActionBar(mToolbar) supportActionBar!!.setDisplayHomeAsUpEnabled(true) // supportActionBar!!.setLogo(resources.getDrawable(R.mipmap.image01)) mToolbar!!.setOnMenuItemClickListener(){ when(it.itemId){ R.id.action_bar -> {Toast.makeText(this, "设置", Toast.LENGTH_SHORT).show(); true} R.id.share_bar -> {Toast.makeText(this, "分享", Toast.LENGTH_SHORT).show(); true} else -> true } } mDrawerLayout = findViewById(R.id.draw_layout) val mDrawerToggle:ActionBarDrawerToggle = ActionBarDrawerToggle(this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name) mDrawerToggle.syncState() mDrawerLayout!!.setDrawerListener(mDrawerToggle) } override fun onCreateOptionsMenu(menu: Menu?): Boolean { menuInflater.inflate(R.menu.main, menu) return true } }当然,如果你了解过Android Studio初始的创建模板中的类似的这个,那么大可不必,使用这个了,不怎么好玩233。