[Android] SlidingMenu使用筆記

[Android] SlidingMenu使用筆記

說明:

Sliding Menu在手機的應用程式中是相當常見的設計,除了以前提到的
Navigation Drawer作法之外,還可以使用本文所提的Sliding Menu(jfeinstein10)的第三方資源來達成。
該資源提供豐富設定供開發者來使用,甚至有左右兩方的選單可以設定。

成果:




程式碼:

package com.chengyu.actionbartest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;
public class ActionBarActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// slidingMenu settings
slidingMenuSet();
setContentView(R.layout.activity_action_bar);
}
private void slidingMenuSet() {
setTitle("SlidingMenu");
SlidingMenu menu = new SlidingMenu(this);
menu.setMenu(R.layout.left_menu);
menu.setMode(SlidingMenu.LEFT);
menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
//menu.setShadowWidthRes(R.dimen.shadow_width);
//menu.setShadowDrawable(R.drawable.shadow);
menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menu.setFadeDegree(0.35f);
menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_action_bar, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
<dimen name="shadow_width">15dp</dimen>
<dimen name="slidingmenu_offset">60dp</dimen>
</resources>
view raw dimension.xml hosted with ❤ by GitHub
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
android:padding="30dp"
android:background="#837e78">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Left Menu" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button3" />
</LinearLayout>
view raw left_menu.xml hosted with ❤ by GitHub

備註:

在project中需要導入第三方library。
有需要與時間的話,再來追加影片與說明。

留言