標籤:des android style blog http color io os ar
在上一篇博文中已經簡單的實現了側滑菜單,代碼也很簡單,就幾行代碼。
這篇文章依然講側滑菜單,與前一篇文章不同的是,這篇文章用不同的代碼方式來實現側滑菜單。
在前面的文章中已經用了在Activity中通過SlidingMenu構造方法直接設定側滑菜單,這裡換成通過Activity繼承SlidingActivity來實現側滑。
代碼如下:
public class MainActivity extends SlidingActivity
重寫onCreate()方法:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setBehindContentView(R.layout.slidingmenu);// 設定側滑布局 SlidingMenu menu = getSlidingMenu();// 擷取SlidingMenu menu.setMode(SlidingMenu.LEFT); // 設定觸控螢幕幕的模式 menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN); // 設定滑動菜單視圖的寬度 menu.setBehindOffsetRes(R.dimen.slidingmenu_offset); // 設定漸入漸出效果的值 menu.setFadeDegree(0.35f); }
其餘的跟上一篇博文的一樣。
運行結果如下:
跟上一篇博文的效果完全一樣。
另外還有一種方式,就是把SlidingMenu當作一般的控制項使用,在XML布局中直接使用即可。
方式如下:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.jeremyfeinstein.slidingmenu.lib.SlidingMenu xmlns:sliding="http://schemas.android.com/apk/res-auto" android:id="@+id/slidingmenulayout" android:layout_width="120dp" android:layout_height="170dp" android:background="#ffffffff" sliding:behindOffset="0dp" sliding:behindScrollScale="1" sliding:fadeDegree="0.3" sliding:fadeEnabled="true" sliding:touchModeAbove="fullscreen" sliding:viewAbove="@layout/pic" > </com.jeremyfeinstein.slidingmenu.lib.SlidingMenu></LinearLayout>
還需要其它的布局檔案:
pic.xml:(其實就是放一張圖片)
<?xml version="1.0" encoding="utf-8"?><ImageView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dp" android:layout_height="200dp" android:src="@drawable/ic_launcher" />
slidingmenu.xml:(側滑菜單的布局檔案)
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#ff999999" android:orientation="vertical" > <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="00000" /></LinearLayout>
接下來是在Activity中的代碼了:
@Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); slidingmenu = (SlidingMenu) findViewById(R.id.slidingmenulayout); slidingmenu.setMode(SlidingMenu.LEFT); // 設定側滑布局 slidingmenu.setMenu(R.layout.slidingmenu); slidingmenu.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if (slidingmenu.isMenuShowing()) slidingmenu.toggle(); } }); }
運行結果如下:
下面是SlidingMenu的常用屬性:
viewAbove - a reference to the layout that you want to use as the above view of the SlidingMenu
viewBehind - a reference to the layout that you want to use as the behind view of the SlidingMenu
touchModeAbove - an enum that designates what part of the screen is touchable when the above view is showing. Margin means only the left margin. Fullscreen means the entire screen. Default is margin.
behindOffset - a dimension representing the number of pixels that you want the above view to show when the behind view is showing. Default is 0.
behindWidth - a dimension representing the width of the behind view. Default is the width of the screen (equivalent to behindOffset = 0).
behindScrollScale - a float representing the relationship between the above view scrolling and the behind behind view scrolling. If set to 0.5f, the behind view will scroll 1px for every 2px that the above view scrolls. If set to 1.0f, the behind view will scroll 1px for every 1px that the above view scrolls. And if set to 0.0f, the behind view will never scroll; it will be static. This one is fun to play around with. Default is 0.25f.
shadowDrawable - a reference to a drawable to be used as a drop shadow from the above view onto the below view. Default is no shadow for now.
shadowWidth - a dimension representing the width of the shadow drawable. Default is 0.
fadeEnabled - a boolean representing whether or not the behind view should fade when the SlidingMenu is closing and "un-fade" when opening
fadeDegree - a float representing the "amount" of fade. 1.0f would mean fade all the way to black when the SlidingMenu is closed. 0.0f would mean do not fade at all.
selectorEnabled - a boolean representing whether or not a selector should be drawn on the left side of the above view showing a selected view on the behind view.
selectorDrawable - a reference to a drawable to be used as the selector NOTE : in order to have the selector drawn, you must call SlidingMenu.setSelectedView(View v) with the selected view. Note that this will most likely not work with items in a ListView because of the way that Android recycles item views.
還有一些到時候再查吧。
Android 側滑菜單的簡單實現(SlidingMenu)二