2015-04-21 51 views
2

Android初學者在Android應用程序開發。我有實現的Android定製app.I操作欄對自定義操作巴爾加2個圖像見下面的XML代碼如何打開和關閉導航抽屜點擊Android中的自定義操作欄圖標?

custom.xml 

    <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="@drawable/black_pattern" > 

     <TextView 
      android:id="@+id/title_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:textAllCaps="true" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textColor="#fff" 
      android:textStyle="bold" /> 

     <ImageView 
      android:id="@+id/imageView1" 
      android:layout_width="35dp" 
      android:layout_height="35dp" 
      android:layout_alignParentLeft="true" 
      android:layout_centerVertical="true" 
      android:layout_marginLeft="8dp" 
      android:src="@drawable/ic_launcher" /> 

     <ImageButton 
      android:id="@+id/imageButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentRight="true" 
      android:layout_centerVertical="true" 
      android:layout_marginRight="8dp" 
      android:background="@null" 
      android:src="@android:drawable/ic_menu_rotate" /> 

    </RelativeLayout> 

並將此XML文件中的動作條視圖

 @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      ActionBar mActionBar = getActionBar(); 
      mActionBar.setDisplayShowHomeEnabled(false); 
      mActionBar.setDisplayShowTitleEnabled(false); 
      LayoutInflater mInflater = LayoutInflater.from(this); 

      View mCustomView = mInflater.inflate(R.layout.custom_actionbar, null); 
      TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.title_text); 
      mTitleTextView.setText("My Own Title"); 

      ImageButton imageButton = (ImageButton) mCustomView 
        .findViewById(R.id.imageButton); 
      imageButton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View view) { 
        Toast.makeText(getApplicationContext(), "Refresh Clicked!", 
          Toast.LENGTH_LONG).show(); 
       } 
      }); 

      mActionBar.setCustomView(mCustomView); 
      mActionBar.setDisplayShowCustomEnabled(true); 
     } 
    } 

但是現在我想在imageButton上打開和關閉導航抽屜,點擊custom.xml中包含的佈局並添加到動作baar中。我實現導航抽屜還可以,但我不明白如何顯示和隱藏的ImageButton導航抽屜單擊event.Can有人幫如何做到這一點

我試着這樣

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mTitle = "test"; 

     mPlanetTitles = new String[]{"one", "two", "three"}; 
     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.left_drawer); 

     // Set the adapter for the list view 
     mDrawerList.setAdapter(new ArrayAdapter<String>(this, 
       R.layout.drawer_list_item, mPlanetTitles)); 
     // Set the list's click listener 
     mDrawerList.setOnItemClickListener(new DrawerItemClickListener()); 

     mDrawerToggle = new ActionBarDrawerToggle(
       this,     /* host Activity */ 
       mDrawerLayout,   /* DrawerLayout object */ 
       R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */ 
       R.string.drawer_open, /* "open drawer" description */ 
       R.string.drawer_close /* "close drawer" description */ 
     ) { 

      /** Called when a drawer has settled in a completely closed state. */ 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
      } 

      /** Called when a drawer has settled in a completely open state. */ 
      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mTitle); 
      } 
     }; 

     // Set the drawer toggle as the DrawerListener 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     getSupportActionBar().setHomeButtonEnabled(true); 


    } 

    @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 
    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); 
    } 
+0

難道ÿ你嘗試調用drawerLayout.openDrawer(int gravity)上的圖像按鈕點擊? –

+0

不,但嘗試過另一種方式。你能看到我上面的帖子。 – JAndroid

回答

2

,如果你有抽屜式導航欄來實現,你可以嘗試:

NavigationDrawerFragment mNavigationDrawerFragment; 
DrawerLayout mDrawerLayout; 

mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(R.id.navigation_drawer); 
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, mDrawerLayout); 

ImageButton imageButton = (ImageButton) mCustomView 
        .findViewById(R.id.imageButton); 
      imageButton.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View view) { 
        Toast.makeText(getApplicationContext(), "Refresh Clicked!", 
          Toast.LENGTH_LONG).show(); 
        if(mDrawerLayout.isDrawerOpen(Gravity.END)) 
         mDrawerLayout.closeDrawer(Gravity.START); 
        else 
         mDrawerLayout.openDrawer(Gravity.END); 
       } 
      }); 
+3

而對於兼容版本: \t \t如果(mDrawerLayout.isDrawerVisible(GravityCompat.START)){ mDrawerLayout.closeDrawer (GravityCompat.START); } else { mDrawerLayout.openDrawer(GravityCompat.START); } – ODAXY

+0

你的XML如何? 'NavigationDrawerFragment'是否是本地的? – Leonardo

0

創建一個抽屜佈局

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


<!-- List of Actions (pages) --> 


    <ListView android:layout_height="match_parent" 
     android:layout_width="280dp" 
     android:id="@+id/navList" 
     android:background="#ffffffff" 
     android:choiceMode="singleChoice" 
     android:layout_below="@+id/profileBox"/> 
    </RelativeLayout> 

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



    private DrawerLayout drawer; 
    // DrawerLayout 
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    ListView mDrawerList = (ListView) findViewById(R.id.navList); 
    Add the drawer content to the listview 
    and use 
    drawer.openDrawer(Gravity.LEFT); 
    drawer.openDrawer(Gravity.LEFT); 
    To open and close the drwaer on imge click 
+0

你能不能詳細介紹我的上述代碼。 – JAndroid

+0

http://codetheory.in/android-navigation-drawer/有一個custtome導航抽屜的例子,在那個鏈接和工作正常,你可以編輯它容易與你的願望 –

+0

在你的代碼,你可以使用mDrawerLayout .openDrawer(Gravity 。左),甚至在activty打開導航抽屜上的任何:) –

相關問題