天天看點

drawableLayout的使用(轉載講的比較清晰的文章)

建立drawbler的布局檔案初始化drawbler的清單

響應drawable清單點選事件

drawableLayout的使用(轉載講的比較清晰的文章)

現在側滑菜單使用很多,大都是通過SlidingMenu實作。現在也可以通過DrawerLayout

建立抽屜布局

frament_content.xml

drawableLayout的使用(轉載講的比較清晰的文章)

<?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" >  

    <TextView  

        android:id="@+id/textView"  

        android:layout_width="match_parent"  

        android:layout_height="wrap_content"  

        android:textSize="25sp" />  

</LinearLayout>  

activity_main.xml

drawableLayout的使用(轉載講的比較清晰的文章)

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"  

    android:id="@+id/drawer_layout"  

    android:layout_height="match_parent" >  

    <!-- The main content view -->  

    <FrameLayout  

        android:id="@+id/content_frame"  

        android:layout_height="match_parent" >  

    </FrameLayout>  

    <!-- The navigation view -->  

    <ListView  

        android:id="@+id/left_drawer"  

        android:layout_width="240dp"  

        android:layout_height="match_parent"  

        android:layout_gravity="start"  

        android:background="#ffffcc"  

        android:choiceMode="singleChoice"  

        android:divider="@android:color/transparent"  

        android:dividerHeight="0dp" >  

    </ListView>  

</android.support.v4.widget.DrawerLayout>  

然後建立一個類繼承Fragment類

drawableLayout的使用(轉載講的比較清晰的文章)

/** 

 * ContentFragment.java 

 * 版權所有(C) 2015 

 * 建立者:cuiran 2015-1-3 下午3:25:44 

 */  

package com.cayden.drawerlayoutdemo;  

import android.app.Fragment;  

import android.os.Bundle;  

import android.view.LayoutInflater;  

import android.view.View;  

import android.view.ViewGroup;  

import android.widget.TextView;  

 * TODO  

 * @author cuiran 

 * @version 1.0.0 

public class ContentFragment extends Fragment {  

    private TextView textView;  

    @Override  

    public View onCreateView(LayoutInflater inflater, ViewGroup container,  

            Bundle savedInstanceState) {  

        View view = inflater.inflate(R.layout.fragment_content, container, false);  

        textView = (TextView) view.findViewById(R.id.textView);  

        String text = getArguments().getString("text");  

        textView.setText(text);  

        return view;  

    }  

}  

完成Activity代碼

drawableLayout的使用(轉載講的比較清晰的文章)

import java.util.ArrayList;  

import android.app.Activity;  

import android.app.FragmentManager;  

import android.content.Intent;  

import android.content.res.Configuration;  

import android.net.Uri;  

import android.support.v4.app.ActionBarDrawerToggle;  

import android.support.v4.widget.DrawerLayout;  

import android.view.Menu;  

import android.view.MenuItem;  

import android.widget.AdapterView;  

import android.widget.AdapterView.OnItemClickListener;  

import android.widget.ArrayAdapter;  

import android.widget.ListView;  

public class MainActivity extends Activity implements OnItemClickListener {  

    private DrawerLayout mDrawerLayout;  

    private ListView mDrawerList;  

    private ArrayList<String> menuLists;  

    private ArrayAdapter<String> adapter;  

    private ActionBarDrawerToggle mDrawerToggle;  

    private String mTitle;  

    protected void onCreate(Bundle savedInstanceState) {  

        super.onCreate(savedInstanceState);  

        setContentView(R.layout.activity_main);  

        mTitle = (String) getTitle();  

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);  

        mDrawerList = (ListView) findViewById(R.id.left_drawer);  

        menuLists = new ArrayList<String>();  

        for (int i = 0; i < 5; i++)  

            menuLists.add("菜單0" + i);  

        adapter = new ArrayAdapter<String>(this,  

                android.R.layout.simple_list_item_1, menuLists);  

        mDrawerList.setAdapter(adapter);  

        mDrawerList.setOnItemClickListener(this);  

        mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,  

                R.drawable.ic_drawer, R.string.drawer_open,  

                R.string.drawer_close) {  

            @Override  

            public void onDrawerOpened(View drawerView) {  

                super.onDrawerOpened(drawerView);  

                getActionBar().setTitle("請選擇");  

                invalidateOptionsMenu(); // Call onPrepareOptionsMenu()  

            }  

            public void onDrawerClosed(View drawerView) {  

                super.onDrawerClosed(drawerView);  

                getActionBar().setTitle(mTitle);  

                invalidateOptionsMenu();  

        };  

        mDrawerLayout.setDrawerListener(mDrawerToggle);  

        //開啟ActionBar上APP ICON的功能  

        getActionBar().setDisplayHomeAsUpEnabled(true);  

        getActionBar().setHomeButtonEnabled(true);  

    public boolean onPrepareOptionsMenu(Menu menu) {  

        boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);  

        menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen);  

        return super.onPrepareOptionsMenu(menu);  

    public boolean onCreateOptionsMenu(Menu menu) {  

        // Inflate the menu; this adds items to the action bar if it is present.  

        getMenuInflater().inflate(R.menu.main, menu);  

        return true;  

    public boolean onOptionsItemSelected(MenuItem item) {  

        //将ActionBar上的圖示與Drawer結合起來  

        if (mDrawerToggle.onOptionsItemSelected(item)){  

            return true;  

        }  

        switch (item.getItemId()) {  

        case R.id.action_websearch:  

            Intent intent = new Intent();  

            intent.setAction("android.intent.action.VIEW");  

            Uri uri = Uri.parse("http://www.baidu.com");  

            intent.setData(uri);  

            startActivity(intent);  

            break;  

        return super.onOptionsItemSelected(item);  

    protected void onPostCreate(Bundle savedInstanceState) {  

        super.onPostCreate(savedInstanceState);  

        //需要将ActionDrawerToggle與DrawerLayout的狀态同步  

        //将ActionBarDrawerToggle中的drawer圖示,設定為ActionBar中的Home-Button的Icon  

        mDrawerToggle.syncState();  

    public void onConfigurationChanged(Configuration newConfig) {  

        super.onConfigurationChanged(newConfig);  

        mDrawerToggle.onConfigurationChanged(newConfig);  

    public void onItemClick(AdapterView<?> arg0, View arg1, int position,  

            long arg3) {  

        // 動态插入一個Fragment到FrameLayout當中  

        Fragment contentFragment = new ContentFragment();  

        Bundle args = new Bundle();  

        args.putString("text", menuLists.get(position));  

        contentFragment.setArguments(args);  

        FragmentManager fm = getFragmentManager();  

        fm.beginTransaction().replace(R.id.content_frame, contentFragment)  

                .commit();  

        mDrawerLayout.closeDrawer(mDrawerList);  

}  

<a href="http://www.wiz.cn/i/ec2c69a8">來自為知筆記(Wiz)</a>

繼續閱讀