android, NavigationDrawer Tutorial |
Layout
You should use DrawerLayout to enable your app edge activated for open drawer automatically.
Otherwise you have to implement drawer open touch event by yourself.
You must keep in mind that FrameLayout that is responsible for drawer has to be come last.
Because android draws xml declared layout from the bottom so that the drawer will be come most upper part.
xml
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#666"
android:dividerHeight="1dp"
android:background="#333"
android:paddingLeft="15sp"
android:paddingRight="15sp"
/>
</android.support.v4.widget.DrawerLayout>
Java
One thing very important is ActionBarDrawerToggle class.
That class is responsible for the setting and manipulation of the Drawer.
// refer : http://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mAactionBarDrawerToggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById( R.id.drawer_layout );
mAactionBarDrawerToggle = new ActionBarDrawerToggle( this, mDrawerLayout, R.drawable.ic_drawer, 0, 0 );
mDrawerLayout.setDrawerListener( mAactionBarDrawerToggle );
mDrawerLayout.setDrawerShadow( R.drawable.drawer_shadow, GravityCompat.START );
getActionBar().setDisplayHomeAsUpEnabled( true );
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mAactionBarDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mAactionBarDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if ( mAactionBarDrawerToggle.onOptionsItemSelected( item ) )
return true;
return super.onOptionsItemSelected(item);
}
}
'프로그래밍 놀이터 > 안드로이드, Java' 카테고리의 다른 글
[android] xxhdpi 는 뭐 하는 녀석이야?? (0) | 2013.12.04 |
---|---|
[Java] Reflection Tutorial - Getter and Setter (0) | 2013.12.04 |
[Java] Reflection Tutorial - Method (0) | 2013.11.28 |
HDR+ 가 뭐야? ( 4.4 kitkat 킷캣에 적용된 camera algorithm ) (0) | 2013.11.26 |
[android] ActionBar Compat 사용하기. (0) | 2013.11.26 |
댓글