国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 抽屜效果的導航菜單實現代碼實例

2019-12-12 04:12:58
字體:
來源:轉載
供稿:網友

看了很多應用,覺得這種側滑的抽屜效果的菜單很好。

不用切換到另一個頁面,也不用去按菜單的硬件按鈕,直接在界面上一個按鈕點擊,菜單就滑出來,而且感覺能放很多東西。

關于實現,搜索了一下,有如下兩種:

1.用SlidingDrawer:http://developer.android.com/reference/android/widget/SlidingDrawer.html

但是不知道為什么這個類官方不建議再繼續用了:Deprecated since API level 17

2.用DrawerLayout:http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html

Guide在這里:http://developer.android.com/training/implementing-navigation/nav-drawer.html

庫的引用

首先, DrawerLayout這個類是在Support Library里的,需要加上android-support-v4.jar這個包。

然后程序中用時在前面導入import android.support.v4.widget.DrawerLayout;

如果找不到這個類,首先用SDK Manager更新一下Android Support Library,然后在Android SDK/extras/android/support/v4路徑下找到android-support-v4.jar,復制到項目的libs路徑,將其Add to Build Path.

代碼1

布局:

<RelativeLayout 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.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 -->    <!-- main content must be the first element of DrawerLayout because it will be drawn first and drawer must be on top of it -->    <FrameLayout      android:id="@+id/content_frame"      android:layout_width="match_parent"      android:layout_height="match_parent" />    <!-- The navigation drawer -->    <ListView      android:id="@+id/left_drawer"      android:layout_width="240dp"      android:layout_height="match_parent"      android:layout_gravity="left"      android:background="#111"      android:choiceMode="singleChoice"      android:divider="@android:color/transparent"      android:dividerHeight="0dp" />  </android.support.v4.widget.DrawerLayout></RelativeLayout>

DrawerLayout的第一個子元素是主要內容,即抽屜沒有打開時顯示的布局。這里采用了一個FrameLayout,里面什么也沒放。

DrawerLayout的第二個子元素是抽屜中的內容,即抽屜布局,這里采用了一個ListView。

主要的Activity(從官方實例中扒出來的):

package com.example.hellodrawer;import android.os.Bundle;import android.app.Activity;import android.content.res.Configuration;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;import android.support.v4.app.ActionBarDrawerToggle;import android.support.v4.view.GravityCompat;import android.support.v4.widget.DrawerLayout;public class HelloDrawerActivity extends Activity{  private String[] mPlanetTitles;  private DrawerLayout mDrawerLayout;  private ActionBarDrawerToggle mDrawerToggle;  private ListView mDrawerList;  @Override  public void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_hello_drawer);    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    // init the ListView and Adapter, nothing new    initListView();    // set a custom shadow that overlays the main content when the drawer    // opens    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,        GravityCompat.START);    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,        R.drawable.ic_drawer, R.string.drawer_open,        R.string.drawer_close)    {      /** Called when a drawer has settled in a completely closed state. */      public void onDrawerClosed(View view)      {        invalidateOptionsMenu(); // creates call to                      // onPrepareOptionsMenu()      }      /** Called when a drawer has settled in a completely open state. */      public void onDrawerOpened(View drawerView)      {        invalidateOptionsMenu(); // creates call to                      // onPrepareOptionsMenu()      }    };    // Set the drawer toggle as the DrawerListener    mDrawerLayout.setDrawerListener(mDrawerToggle);    // enable ActionBar app icon to behave as action to toggle nav drawer    getActionBar().setDisplayHomeAsUpEnabled(true);    // getActionBar().setHomeButtonEnabled(true);    // Note: getActionBar() Added in API level 11  }  private void initListView()  {    mDrawerList = (ListView) findViewById(R.id.left_drawer);    mPlanetTitles = getResources().getStringArray(R.array.planets_array);    // Set the adapter for the list view    mDrawerList.setAdapter(new ArrayAdapter<String>(this,        R.layout.list_item, mPlanetTitles));    // Set the list's click listener    mDrawerList.setOnItemClickListener(new OnItemClickListener()    {      @Override      public void onItemClick(AdapterView<?> parent, View view,          int position, long id)      {        // Highlight the selected item, update the title, and close the        // drawer        mDrawerList.setItemChecked(position, true);        setTitle(mPlanetTitles[position]);        mDrawerLayout.closeDrawer(mDrawerList);      }    });  }  @Override  protected void onPostCreate(Bundle savedInstanceState)  {    super.onPostCreate(savedInstanceState);    // Sync the toggle state after onRestoreInstanceState has occurred.    mDrawerToggle.syncState();  }  @Override  public void onConfigurationChanged(Configuration newConfig)  {    super.onConfigurationChanged(newConfig);    mDrawerToggle.onConfigurationChanged(newConfig);  }  @Override  public boolean onOptionsItemSelected(MenuItem item)  {    // Pass the event to ActionBarDrawerToggle, if it returns    // true, then it has handled the app icon touch event    if (mDrawerToggle.onOptionsItemSelected(item))    {      return true;    }    // Handle your other action bar items...    return super.onOptionsItemSelected(item);  }}

比較糾結的是用了Level 11的一個API,這樣minSdkVersion就有限制,不能太低。

圖片資源Android官網示例處提供下載了。

程序運行后效果如下:

抽屜打開前:

抽屜打開后:

代碼2

今天又看了一下DrawerLayout的類,發現有很多方法可以直接用的。

重新試了一下,其實不用上面那么麻煩,隨便自己定義一個按鈕控制抽屜的打開就行:

布局:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context=".DrawerActivity" >  <android.support.v4.widget.DrawerLayout    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" >      <Button        android:id="@+id/btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="open"         />    </FrameLayout>    <!-- The navigation drawer -->    <ListView      android:id="@+id/left_drawer"      android:layout_width="240dp"      android:layout_height="match_parent"      android:layout_gravity="start"      android:background="#111"      android:choiceMode="singleChoice"      android:divider="@android:color/transparent"      android:dividerHeight="0dp" />  </android.support.v4.widget.DrawerLayout></RelativeLayout>

主要代碼:

package com.example.hellodrawer;import android.os.Bundle;import android.app.Activity;import android.support.v4.widget.DrawerLayout;import android.view.Gravity;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;public class DrawerActivity extends Activity{  private DrawerLayout mDrawerLayout = null;  @Override  protected void onCreate(Bundle savedInstanceState)  {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_drawer);    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);    Button button = (Button) findViewById(R.id.btn);    button.setOnClickListener(new OnClickListener()    {      @Override      public void onClick(View v)      {        // 按鈕按下,將抽屜打開        mDrawerLayout.openDrawer(Gravity.LEFT);      }    });  }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 普兰店市| 翼城县| 云梦县| 通榆县| 大冶市| 和顺县| 呼和浩特市| 宝坻区| 白朗县| 锡林浩特市| 阿拉尔市| 亳州市| 永春县| 亳州市| 德清县| 沂源县| 佳木斯市| 长治市| 波密县| 博兴县| 遵化市| 洪雅县| 乌审旗| 唐河县| 日喀则市| 耿马| 邵阳市| 长宁县| 枣阳市| 屯昌县| 沾益县| 桃园县| 育儿| 永修县| 肇东市| 丰镇市| 揭西县| 睢宁县| 友谊县| 广水市| 巩留县|