0

我有一個導航抽屜,我試圖改變它的開放位置。我希望我的導航抽屜在右側打開。安卓導航抽屜右側不工作

這是我的導航抽屜XML代碼:

<ge.me.custom.CustomDrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/drawer_layout5" 
android:layout_width="200dp" 
android:layout_height="match_parent" > 
<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
<!-- The navigation drawer --> 
<LinearLayout 
    android:id="@+id/drawer" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="start" 
    android:background="#09b52f" 
    android:orientation="vertical" > 

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

     <RelativeLayout 
      android:id="@+id/slidemenumainlayout" 
      android:layout_width="match_parent" 
      android:layout_height="120dp" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentTop="true" > 

      <ImageView 
       android:id="@+id/slidemenuuserimage" 
       android:layout_width="70dp" 
       android:layout_height="70dp" 
       android:layout_alignParentBottom="true" 
       android:layout_alignParentLeft="true" 
       android:layout_marginBottom="16dp" 
       android:layout_marginLeft="14dp" 
       android:background="@drawable/ic_user_blue" /> 

     </RelativeLayout> 

     <ListView 
      android:id="@+id/drawer_list" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignRight="@+id/slidemenumainlayout" 
      android:layout_below="@+id/slidemenumainlayout" 
      android:divider="#00af25" 
      android:dividerHeight="1dp" > 
     </ListView> 
    </RelativeLayout> 
</LinearLayout> 

這是我的自定義抽屜:

public class CustomDrawerLayout extends DrawerLayout { 

    public CustomDrawerLayout(Context context) { 
     super(context); 
    } 

    public CustomDrawerLayout(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomDrawerLayout(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.EXACTLY); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(
      MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.EXACTLY); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 

} 

而且這段代碼的主要活動中:

mDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, 
      R.drawable.ic_drawer, R.string.app_name, 
      R.string.app_name) { 

     @Override 
     public boolean onOptionsItemSelected(MenuItem item) { 
      if (item != null && item.getItemId() == android.R.id.home) { 
       if (mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) { 
        mDrawerLayout.closeDrawer(Gravity.RIGHT); 
       } else { 
        mDrawerLayout.openDrawer(Gravity.RIGHT); 
       } 
      } 
      return false; 
     } 
    }; 

    mDrawerLayout.setDrawerListener(mDrawerToggle); 

I寫了一個方法來打開我的抽屜在右側,但是當我運行我的應用程序時,它崩潰了。

public void openDrawer(int gravity) { 
    mDrawerLayout.openDrawer(gravity); 
} 
openDrawer(Gravity.RIGHT); 

如果我把這樣的方法:

openDrawer(Gravity.START); 

我沒有得到一個錯誤,但抽屜式導航欄的起始位置是左。

這是我的錯誤:

java.lang.IllegalArgumentException: No drawer view found with gravity RIGHT 

我怎樣才能打開右側的導航抽屜?

+0

您是否嘗試過'Gravity.END'? –

+0

我試過了,但我有同樣的錯誤@ Noob Coder – BekaKK

回答

0

嘗試在你的XML設置layout_gravityright

<LinearLayout 
    android:id="@+id/drawer" 
    android:layout_width="200dp" 
    android:layout_height="match_parent" 
    android:layout_gravity="right" 
    android:background="#09b52f" 
    android:orientation="vertical" > 
0

你應該堅持在菜單的項目點擊更簡單的代碼。我認爲您不必擔心抽屜被關閉或不關閉,因爲抽屜打開時無法觸及圖標。

簡單的解決辦法是這樣的:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    if (item.getItemId() == android.R.id.home) { 
     mDrawerLayout.openDrawer(Gravity.RIGHT); 
    } 
    return false; 
}); 
+0

謝謝,但不工作。我不知道什麼是錯的 – BekaKK