Android實現網易新聞客戶端側滑菜單(1)
Android中很多產品(比如360手機助手、網易菜單...)都采用側滑菜單的展現形式,采用這種展現形式
1、能把更多的展現內容都存放在菜單中
2、設計上也能體現出視覺效果
現在這種交互方式越來越流行了,雖然這種交互方式可以通過自定義組件的方式來實現,但是用三方開源庫更簡單。
SlidingMenu:SlidingMenu的是一種比較新的設置界面或配置界面效果,在主界面左滑或者右滑出現設置界面,能方便的進行各種操作.目前有大量的應用都在使用這一效果。
地址:https://github.com/jfeinstein10/SlidingMenu.git
現在新建一個Android項目SlidingMenuDemo,
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="fill_parent" android:layout_height="fill_parent" android:text="內容" android:textSize="24sp" android:textColor="#000" android:gravity="center" /> </RelativeLayout>
左邊的菜單activity_menu:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:text="體育" android:textSize="16sp" android:textColor="#000" /> <TextView android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:text="娛樂" android:textSize="16sp" android:textColor="#000" /> <TextView android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:text="財經" android:textSize="16sp" android:textColor="#000" /> <TextView android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:text="科技" android:textSize="16sp" android:textColor="#000" /> <TextView android:layout_width="fill_parent" android:layout_height="40dp" android:gravity="center" android:text="杭州" android:textSize="16sp" android:textColor="#000" /> </LinearLayout>
MainActivity.java:
package com.example.slidingmenudemo;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
import com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;
import android.os.Bundle;
import android.view.Window;
import android.app.Activity;
public class MainActivity extends SlidingFragmentActivity {
private SlidingMenu sm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 1 設置滑動菜單旁邊的頁面
setBehindContentView(R.layout.activity_menu);
setContentView(R.layout.activity_main);
//2 獲取滑動菜單
sm = getSlidingMenu();
//3 設置左滑菜單
sm.setMode(SlidingMenu.LEFT);
// 4 設置滑動菜單出來之后,內容頁剩余的寬度
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
// 6 設置陰影
sm.setShadowDrawable(R.drawable.shadow);
sm.setShadowWidthRes(R.dimen.shadow_width);
}
}
注意Activity一定要繼承SlidingFragmentActivity。
現在看看SlidingMenu一些屬性:
menu.setMode(SlidingMenu.LEFT);//設置左滑菜單 menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設置滑動的屏幕范圍,該設置為全屏區(qū)域都可以滑動 menu.setShadowDrawable(R.drawable.shadow);//設置陰影圖片 menu.setShadowWidthRes(R.dimen.shadow_width);//設置陰影圖片的寬度 menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩余寬度 menu.setBehindWidth(400);//設置SlidingMenu菜單的寬度 menu.setFadeDegree(0.35f);//SlidingMenu滑動時的漸變程度 menu.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);//使SlidingMenu附加在Activity上 menu.setMenu(R.layout.menu_layout);//設置menu的布局文件 menu.toggle();//動態(tài)判斷自動關閉或開啟SlidingMenu menu.showMenu();//顯示SlidingMenu menu.showContent();//顯示內容
左右都可以劃出SlidingMenu菜單只需要設置
menu.setMode(SlidingMenu.LEFT_RIGHT);屬性,然后設置右側菜單的布局文件 menu.setSecondaryShadowDrawable(R.drawable.shadowright);//右側菜單的陰影圖片
設置SlidingMenu屬性
sm = getSlidingMenu(); //如果只顯示左側菜單就是用LEFT,右側就RIGHT,左右都支持就LEFT_RIGHT sm.setMode(SlidingMenu.LEFT_RIGHT);//設置菜單滑動模式,菜單是出現在左側還是右側,還是左右兩側都有 sm.setShadowDrawable(R.drawable.shadow);//設置陰影的圖片資源 sm.setShadowWidthRes(R.dimen.shadow_width);//設置陰影圖片的寬度 //sm.setBehindWidth(200);//設置菜單的寬 sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);//SlidingMenu劃出時主頁面顯示的剩余寬度 sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);//設置滑動的區(qū)域
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android開發(fā)必備:秒殺真機超快模擬器Genymotion介紹
這篇文章主要介紹了Android開發(fā)必備:秒殺真機超快模擬器Genymotion介紹,本文直接用圖片說明Genymotion的安裝和模擬效果,并提供官網,需要的朋友可以參考下2015-04-04
Android RecyclerView使用GridLayoutManager間距設置的方法
本篇文章主要介紹了Android RecyclerView使用GridLayoutManager間距設置的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12
Android中創(chuàng)建一個透明的進度對話框實例
這篇文章主要介紹了Android中創(chuàng)建一個透明的進度對話框實例,需要的朋友可以參考下2014-05-05
Android自定義ViewPager實現個性化的圖片切換效果
這篇文章主要介紹了Android自定義ViewPager實現個性化的圖片切換效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05
Android中SurfaceView和普通view的區(qū)別及使用
SurfaceView第一印象它是一個view,因為它繼承了View,本文主要介紹了SurfaceView和普通view的區(qū)別及使用,感興趣的小伙伴們可以參考一下2021-06-06
android使用 ScrollerView 實現 可上下滾動的分類欄實例
本篇文章主要介紹了android使用 ScrollerView 實現 可上下滾動的分類欄實例,具有一定的參考價值,有興趣的可以了解一下。2017-01-01

