網站首頁 編程語言 正文
本文實例為大家分享了Android實現左側滑動菜單的具體代碼,供大家參考,具體內容如下
效果圖:
SlideActivity.java:
package com.demo.slide; ? import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.Window; ? import com.demo.broadcast.R; ? public class SlideActivity extends Activity { ? ?? ?private SlidingMenu mLeftMenu ;? ?? ? ?? ?@Override ?? ?protected void onCreate(Bundle savedInstanceState) ?? ?{ ?? ??? ?super.onCreate(savedInstanceState); ?? ??? ?requestWindowFeature(Window.FEATURE_NO_TITLE); ?? ??? ?setContentView(R.layout.slide_main); ?? ??? ? ?? ??? ?mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu); ?? ?} ? ?? ?public void toggleMenu(View view) ?? ?{ ?? ??? ?mLeftMenu.toggle(); ?? ?} }
SlidingMenu.java:
package com.demo.slide; ? import android.content.Context; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; import android.view.MotionEvent; import android.view.ViewGroup; import android.view.WindowManager; import android.widget.HorizontalScrollView; import android.widget.LinearLayout; ? public class SlidingMenu extends HorizontalScrollView { ?? ?private LinearLayout mWapper; ?? ?private ViewGroup mMenu; ?? ?private ViewGroup mContent; ?? ?private int mScreenWidth; ? ?? ?private int mMenuWidth; ?? ?// dp ?? ?private int mMenuRightPadding = 80; ? ?? ?private boolean once; ? ?? ?private boolean isOpen; ? ?? ?/** ?? ? * 未使用自定義屬性時,調用 ?? ? *? ?? ? * @param context ?? ? * @param attrs ?? ? */ ?? ?public SlidingMenu(Context context, AttributeSet attrs) ?? ?{ ?? ??? ?this(context, attrs, 0); ?? ?} ? ?? ?/** ?? ? * 當使用了自定義屬性時,會調用此構造方法 ?? ? *? ?? ? * @param context ?? ? * @param attrs ?? ? * @param defStyle ?? ? */ ?? ?public SlidingMenu(Context context, AttributeSet attrs, int defStyle) ?? ?{ ?? ??? ?super(context, attrs, defStyle); ? ?? ??? ?WindowManager wm = (WindowManager) context ?? ??? ??? ??? ?.getSystemService(Context.WINDOW_SERVICE); ?? ??? ?DisplayMetrics outMetrics = new DisplayMetrics(); ?? ??? ?wm.getDefaultDisplay().getMetrics(outMetrics); ?? ??? ?mScreenWidth = outMetrics.widthPixels; ?? ??? ?mMenuRightPadding = (int) TypedValue.applyDimension( ?? ??? ??? ??? ?TypedValue.COMPLEX_UNIT_DIP, 50, context ?? ??? ??? ??? ?.getResources().getDisplayMetrics()); ?? ?} ? ?? ?public SlidingMenu(Context context) ?? ?{ ?? ??? ?this(context, null); ?? ?} ? ?? ?/** ?? ? * 設置子View的寬和高 設置自己的寬和高 ?? ? */ ?? ?@Override ?? ?protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) ?? ?{ ?? ??? ?if (!once) ?? ??? ?{ ?? ??? ??? ?mWapper = (LinearLayout) getChildAt(0); ?? ??? ??? ?mMenu = (ViewGroup) mWapper.getChildAt(0); ?? ??? ??? ?mContent = (ViewGroup) mWapper.getChildAt(1); ?? ??? ??? ? ?? ??? ??? ?mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth ?? ??? ??? ??? ??? ?- mMenuRightPadding; ?? ??? ??? ?mContent.getLayoutParams().width = mScreenWidth; ?? ??? ??? ?once = true; ?? ??? ?} ?? ??? ?super.onMeasure(widthMeasureSpec, heightMeasureSpec); ?? ?} ? ?? ?/** ?? ? * 通過設置偏移量,將menu隱藏 ?? ? */ ?? ?@Override ?? ?protected void onLayout(boolean changed, int l, int t, int r, int b) ?? ?{ ?? ??? ?super.onLayout(changed, l, t, r, b); ?? ??? ?if (changed) ?? ??? ?{ ?? ??? ??? ?this.scrollTo(mMenuWidth, 0); ?? ??? ?} ?? ?} ? ?? ?@Override ?? ?public boolean onTouchEvent(MotionEvent ev) ?? ?{ ?? ??? ?int action = ev.getAction(); ?? ??? ?switch (action) ?? ??? ?{ ?? ??? ?case MotionEvent.ACTION_UP: ?? ??? ??? ?// 隱藏在左邊的寬度 ?? ??? ??? ?int scrollX = getScrollX(); ?? ??? ??? ?if (scrollX >= mMenuWidth / 2) ?? ??? ??? ?{ ?? ??? ??? ??? ?this.smoothScrollTo(mMenuWidth, 0); ?? ??? ??? ??? ?isOpen = false; ?? ??? ??? ?} else ?? ??? ??? ?{ ?? ??? ??? ??? ?this.smoothScrollTo(0, 0); ?? ??? ??? ??? ?isOpen = true; ?? ??? ??? ?} ?? ??? ??? ?return true; ?? ??? ?} ?? ??? ?return super.onTouchEvent(ev); ?? ?} ? ?? ?/** ?? ? * 打開菜單 ?? ? */ ?? ?public void openMenu() ?? ?{ ?? ??? ?if (isOpen) ?? ??? ??? ?return; ?? ??? ?this.smoothScrollTo(0, 0); ?? ??? ?isOpen = true; ?? ?} ? ?? ?public void closeMenu() ?? ?{ ?? ??? ?if (!isOpen) ?? ??? ??? ?return; ?? ??? ?this.smoothScrollTo(mMenuWidth, 0); ?? ??? ?isOpen = false; ?? ?} ? ?? ?/** ?? ? * 切換菜單 ?? ? */ ?? ?public void toggle() ?? ?{ ?? ??? ?if (isOpen) ?? ??? ?{ ?? ??? ??? ?closeMenu(); ?? ??? ?} else ?? ??? ?{ ?? ??? ??? ?openMenu(); ?? ??? ?} ?? ?} }
slide_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" > ? ? <com.demo.slide.SlidingMenu ? ? ? ? android:id="@+id/id_menu" ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="match_parent" > ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:orientation="horizontal" > ? ? ? ? ? ? <include layout="@layout/left_menu" /> ? ? ? ? ? ? <LinearLayout ? ? ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? ? ? android:background="#ffffff" > ? ? ? ? ? ? ? ? <Button ? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? ? ? android:onClick="toggleMenu" ? ? ? ? ? ? ? ? ? ? android:text="切換" /> ? ? ? ? ? ? ? ? <TextView ? ? ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? ? ? ? ? android:layout_marginLeft="20dp" ? ? ? ? ? ? ? ? ? ? android:text="我是主content" ? ? ? ? ? ? ? ? ? ? android:textColor="#ff00ff" ? ? ? ? ? ? ? ? ? ? android:textSize="20sp" /> ? ? ? ? ? ? </LinearLayout> ? ? ? ? </LinearLayout> ? ? </com.demo.slide.SlidingMenu> </RelativeLayout>
left_menu.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent" ? ? android:background="#000000" > ? ? <LinearLayout ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:layout_centerInParent="true" ? ? ? ? android:orientation="vertical" > ? ? ? ? <TextView ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? android:layout_marginLeft="20dp" ? ? ? ? ? ? android:text="我是左側Menu" ? ? ? ? ? ? android:textColor="#ffffff" ? ? ? ? ? ? android:textSize="20sp" /> ? ? </LinearLayout> </RelativeLayout>
原文鏈接:https://blog.csdn.net/chenzheng8975/article/details/84690431
相關推薦
- 2022-06-26 Android?app啟動節點與上報啟動實例詳解_Android
- 2023-03-16 淺析Kotlin使用infix函數構建可讀語法流程講解_Android
- 2022-12-13 Android?itemDecoration接口實現吸頂懸浮標題_Android
- 2022-08-15 linux交叉編譯依賴包
- 2022-02-20 Android?WebView開發之WebView與Native交互_Android
- 2022-04-16 python字符串不可變數據類型_python
- 2022-07-08 python中的type,元類,類,對象用法_python
- 2022-03-03 uniapp的報錯ncaught Error: Module build failed (from
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支