知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
Android側滑菜單DrawerLayout的使用
發(fā)表時間:2020-10-19
發(fā)布人:葵宇科技
瀏覽次數(shù):37
[img]http://img.my.csdn.net/uploads/201501/03/1420281999_5098.gif
如今側滑菜單應用很多,大年夜都是經(jīng)由過程SlidingMenu實現(xiàn)。如今也可以經(jīng)由過程DrawerLayout
創(chuàng)建抽屜構造
frament_content.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" > <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="25sp" /> </LinearLayout>activity_main.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" > <!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" 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>
然后新建一個類持續(xù)Fragment類
/** * ContentFragment.java * 版權所有(C) 2015 * 創(chuàng)建者: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代碼
package com.cayden.drawerlayoutdemo; import java.util.ArrayList; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.content.Intent; import android.content.res.Configuration; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.ActionBarDrawerToggle; import android.support.v4.widget.DrawerLayout; import android.view.Menu; import android.view.MenuItem; import android.view.View; 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; @Override 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() } @Override public void onDrawerClosed(View drawerView) { super.onDrawerClosed(drawerView); getActionBar().setTitle(mTitle); invalidateOptionsMenu(); } }; mDrawerLayout.setDrawerListener(mDrawerToggle); //開啟ActionBar上APP ICON的功能 getActionBar().setDisplayHomeAsUpEnabled(true); getActionBar().setHomeButtonEnabled(true); } @Override public boolean onPrepareOptionsMenu(Menu menu) { boolean isDrawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList); menu.findItem(R.id.action_websearch).setVisible(!isDrawerOpen); return super.onPrepareOptionsMenu(menu); } @Override 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; } @Override 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); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); //須要將ActionDrawerToggle與DrawerLayout的狀況同步 //將ActionBarDrawerToggle中的drawer擱筆,設置為ActionBar中的Home-Button的Icon mDrawerToggle.syncState(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) { // 動態(tài)插入一個Fragment到FrameLayout傍邊 Fragment contentFragment = new ContentFragment(); Bundle args = new Bundle(); args.putString("text", menuLists.get(position)); contentFragment.setArguments(args); FragmentManager fm = getFragmentManager(); fm.beginTransaction().WordStr(R.id.content_frame, contentFragment) .commit(); mDrawerLayout.closeDrawer(mDrawerList); } }
相關案例查看更多
相關閱讀
- asp網(wǎng)站
- 網(wǎng)站建設高手
- 表單
- 百度快速排名
- 云南網(wǎng)站建設靠譜公司
- 云南旅游網(wǎng)站建設
- 云南小程序開發(fā)課程
- 百度小程序開發(fā)
- 全國前十名小程序開發(fā)公司
- 云南電商網(wǎng)站建設
- 小程序開發(fā)公司
- 云南etc微信小程序
- 網(wǎng)絡公司
- 云南網(wǎng)站建設案例
- 小程序生成海報
- 報廢車
- 云南建設廳網(wǎng)站
- 服務器
- 云南網(wǎng)站建設哪家好
- 網(wǎng)站建設優(yōu)化
- 南通小程序制作公司
- 云南網(wǎng)站建設首選公司
- 大理小程序開發(fā)
- 曲靖小程序開發(fā)
- 電商網(wǎng)站建設
- 昆明軟件公司
- 汽車回收管理
- 云南衛(wèi)視小程序
- 紅河小程序開發(fā)
- 百度推廣