Android - DrawerLayout

摘要:Android - DrawerLayout

這次遇到的是,在第一頁,有需要按上UP鍵,然後出現左邊選單。

 

選單,就要在MainActivity裡設定,而原本的MainContent則另外用一個layout , fragment_main來處理。

所以首頁就變成了MainActivity,另外內容的部分就變成了MainFragment

 

xml,就分,activity_main.xml,fragment_main.xml,還有menu_fram.xml

首先menu_fram.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="@color/drawer_list_bg"
    android:orientation="vertical" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@android:color/white"
            android:orientation="vertical" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:gravity="bottom"
                android:orientation="horizontal"
                android:padding="8dp" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="主標題"
                    android:textColor="@color/parent_color"
                    android:textSize="18sp" />
            </LinearLayout>

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/gray_line" />

            <LinearLayout
                android:id="@+id/ll_intro"
                android:layout_width="match_parent"
                android:layout_height="48dp"
                android:gravity="center_vertical"
                android:orientation="horizontal" 
                android:clickable="true"
                android:background="@drawable/selector_menu">

                <ImageView
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="8dp"
                    android:src="@drawable/list_icon" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="副標題"
                    android:textColor="@color/sub_title_color"
                    android:textSize="22sp" />
            </LinearLayout>
            
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:background="@color/gray_line" />
            
        </LinearLayout>
    </ScrollView>

</LinearLayout>

 

activity_main 如下


<?xml version="1.0" encoding="utf-8"?>
<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">
    <!-- The main content view -->
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    <!-- The navigation drawer -->
    <include 
        android:layout_gravity="start"
        android:drawSelectorOnTop="true" 
        android:id="@+id/left_drawer"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        layout="@layout/menu_fram"/>
</android.support.v4.widget.DrawerLayout>

 

fragment_main.xml 就隨意了。(依需求而定)

 

最主要要寫的會是MainActivity

如下import android.app.ActionBar;


import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;


public class MainActivity extends Activity {    
    private DrawerLayout mDrawerLayout;
    private LinearLayout mDrawerList;
    public static int PAGE_NOW = 0;
    public static final int PAGE_INDEX_MAIN = 0;
    private ActionBarDrawerToggle mDrawerToggle;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.activity_main);
        
        PAGE_NOW = 0;
        
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (LinearLayout) findViewById(R.id.left_drawer);  
        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,GravityCompat.START);
        
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setIcon(R.drawable.bar_icon);
        
        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                R.drawable.ic_drawer, R.string.drawer_open,R.string.drawer_close) {
            public void onDrawerClosed(View view) {
            }

            public void onDrawerOpened(View drawerView) {
            }
        };
        
        selectPage(PAGE_INDEX_MAIN);
    }   

    public void selectPage(int position) { 
       PAGE_NOW = position;

       Fragment fragment = null;
       
       switch (position) {
          case PAGE_INDEX_MAIN:
             fragment = new MainFragment();
             break;
          default:
             break;
       }
       fragment = new MainFragment();
       
       if (fragment != null) {
           FragmentManager fragmentManager = getFragmentManager();
           fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
       }

       mDrawerLayout.closeDrawers();
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {

        return super.onPrepareOptionsMenu(menu);
    }
    
    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }
    

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
        case android.R.id.home:
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
            break;
        default:
            break;
        }
        
        return false;
    }
}