我正在尝试在NavigationDrawer中添加标题图片或图标,但我发现/尝试的所有示例对我的实现都没有用。
尝试将drawerlayout更改为relativeLayout onOptionsItemSelected。
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == android.R.id.home){
if(drawerLayout.isDrawerOpen(navList)){
drawerLayout.closeDrawer(navList);
}
else
drawerLayout.openDrawer(navList);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent,View view,int position, long id) {
loadSelection(position);
drawerLayout.closeDrawer(Gravity.LEFT);
}
错误
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1415)
at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1677)
at acr.acr_app.MenuPrincipalActivity.onOptionsItemSelected(MenuPrincipalActivity.java:161)
at android.app.Activity.onMenuItemSelected(Activity.java:2925)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:192)
at android.view.View.performClick(View.java:4832)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
更新了布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawerlayout">
<FrameLayout
android:paddingTop="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentholder">
</FrameLayout>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/acr_home"/>
<ListView
android:layout_width="230dp"
android:layout_height="match_parent"
android:paddingTop="80dp"
android:id="@+id/navList"
android:background="@drawable/mb04"
android:layout_gravity="start">
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Activity_main
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_principal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
navList = (ListView)findViewById(R.id.navList);
navList.setOnItemClickListener(this);
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Home");
navArray.add("Frag2");
navArray.add("Frag3");
navArray.add("Frag4");
navArray.add("Frag5");
(this,android.R.layout.simple_list_item_1,navArray);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.my_text_view,navArray);
navList.setAdapter(adapter);
actionBarDrawerToogle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.addDrawerListener(actionBarDrawerToogle);
actionBar = getSupportActionBar();
fragmentManager = getSupportFragmentManager();
loadSelection(0);
}
答案 0 :(得分:2)
问题是我没有正确更改哪个布局应该关闭点击。
@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if(id == android.R.id.home){
if(drawerLayout.isDrawerOpen(mDrawerRelativeLayout)){
drawerLayout.closeDrawer(mDrawerRelativeLayout);
}
else
drawerLayout.openDrawer(mDrawerRelativeLayout);
}
return super.onOptionsItemSelected(item);
}
<强>布局强>
<android.support.v4.widget.DrawerLayout 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:background="#e6e6ff"
android:id="@+id/drawerlayout">
<FrameLayout
android:paddingTop="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentholder">
</FrameLayout>
<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ffffff"
android:orientation="vertical" >
<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="130dp"
android:background="@drawable/logodrawerlateral" />
<ListView
android:id="@+id/navList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@id/image_view"
android:choiceMode="singleChoice" />
</RelativeLayout>
答案 1 :(得分:1)
将列表视图放在布局和列表视图上方放置图像视图。请使用此参考http://thedeveloperworldisyours.com/android/put-image-navigation-drawer/#sthash.1l6ynBbz.dpbs