Android DrawerLayout實現(xiàn)側(cè)拉菜單功能
效果圖
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.lenovo.mydrawerlayout.MainActivity"> <android.support.v4.widget.DrawerLayout android:layout_width="match_parent" android:id="@+id/drawer_layout" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是主界面" /> </LinearLayout> <LinearLayout android:layout_width="300dp" android:layout_height="match_parent" android:layout_gravity="left" android:background="#f00" > <TextView android:id="@+id/t" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是側(cè)拉界面" /> </LinearLayout> </android.support.v4.widget.DrawerLayout> </LinearLayout>
注:DrawerLayout下有兩個布局,DrawerLayout的第一個子元素是主要內(nèi)容,即抽屜沒有打開時顯示的布局,DrawerLayout的第二個子元素是抽屜中的內(nèi)容,即抽屜布局,layout_gravity為側(cè)拉方向
因為用到Toolbar所以要把ActionBar替換了所以設(shè)置
values下設(shè)置styles.xml 為NoActionBar
<resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> <!-- 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"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> </resources>
MainActivity
package com.example.lenovo.mydrawerlayout; import android.os.Bundle; import android.support.design.widget.Snackbar; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.View; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Toolbar代替ActionBar Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // DrawerLayout DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); //Toolbar上面最左邊顯示三杠圖標監(jiān)聽DrawerLayout ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.setDrawerListener(toggle); toggle.syncState(); //側(cè)拉頁面字體 TextView t= (TextView) findViewById(R.id.t); t.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v, "側(cè)拉頁面", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); } @Override public void onBackPressed() { DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } } }
- Android實現(xiàn)右邊抽屜Drawerlayout效果
- Android側(cè)滑菜單之DrawerLayout用法詳解
- Android使用DrawerLayout實現(xiàn)仿QQ雙向側(cè)滑菜單
- Android App中DrawerLayout抽屜效果的菜單編寫實例
- Android側(cè)滑菜單控件DrawerLayout使用詳解
- Android中DrawerLayout+ViewPager滑動沖突的解決方法
- Android原生側(cè)滑控件DrawerLayout使用方法詳解
- Android組件之DrawerLayout實現(xiàn)抽屜菜單
- Android中DrawerLayout實現(xiàn)側(cè)滑菜單效果
- Android抽屜布局DrawerLayout的簡單使用
相關(guān)文章
Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯
這篇文章主要介紹了Android中實現(xiàn)淘寶購物車RecyclerView或LIstView的嵌套選擇的邏輯,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12Android開發(fā)ThreadPoolExecutor與自定義線程池詳解
這篇文章主要為大家介紹了Android開發(fā)ThreadPoolExecutor與自定義線程池詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11Android SQLite數(shù)據(jù)庫加密的操作方法
因為Android自帶的SQLite數(shù)據(jù)庫本身是沒有實現(xiàn)加密的,那我們?nèi)绾螌崿F(xiàn)對數(shù)據(jù)庫的加密呢?今天通過本文給大家介紹下Android SQLite數(shù)據(jù)庫加密的操作方法,一起看看吧2021-09-09Android OkHttp實現(xiàn)全局過期token自動刷新示例
本篇文章主要介紹了Android OkHttp實現(xiàn)全局過期token自動刷新示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03Android實現(xiàn)TextView顯示HTML加圖片的方法
這篇文章主要介紹了Android實現(xiàn)TextView顯示HTML加圖片的方法,結(jié)合實例形式分析了TextView控件顯示網(wǎng)絡(luò)圖片的相關(guān)操作技巧,需要的朋友可以參考下2016-07-07